]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Allow clients to specify attrs in decorator.
authorWei-Cheng Pan <legnaleurc@gmail.com>
Tue, 31 Mar 2015 10:15:49 +0000 (18:15 +0800)
committerWei-Cheng Pan <legnaleurc@gmail.com>
Tue, 31 Mar 2015 10:18:15 +0000 (18:18 +0800)
Clients can specify `executor` and `io_loop` attribute name in
`run_on_executor` decorator.

tornado/concurrent.py

index 4fdc4b28514d09b492d800f7184bfe67f4e30bdc..d4bfaa9c425e962e7a0acfbed7d29a078cafaa98 100644 (file)
@@ -337,7 +337,7 @@ class DummyExecutor(object):
 dummy_executor = DummyExecutor()
 
 
-def run_on_executor(fn):
+def run_on_executor(*args, **kwargs):
     """Decorator to run a synchronous method asynchronously on an executor.
 
     The decorated method may be called with a ``callback`` keyword
@@ -346,15 +346,21 @@ def run_on_executor(fn):
     This decorator should be used only on methods of objects with attributes
     ``executor`` and ``io_loop``.
     """
-    @functools.wraps(fn)
-    def wrapper(self, *args, **kwargs):
-        callback = kwargs.pop("callback", None)
-        future = self.executor.submit(fn, self, *args, **kwargs)
-        if callback:
-            self.io_loop.add_future(future,
+    def run_on_executor_decorator(fn):
+        executor = kwargs.get("executor", "executor")
+        io_loop = kwargs.get("io_loop", "io_loop")
+        @functools.wraps(fn)
+        def wrapper(self, *args, **kwargs):
+            callback = kwargs.pop("callback", None)
+            future = getattr(self, executor).submit(fn, self, *args, **kwargs)
+            if callback:
+                getattr(self, io_loop).add_future(future,
                                     lambda future: callback(future.result()))
-        return future
-    return wrapper
+            return future
+        return wrapper
+    if len(args) == 1 and callable(args[0]):
+        return run_on_executor_decorator(args[0])
+    return run_on_executor_decorator
 
 
 _NO_RESULT = object()