]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Clean up and document run_on_executor kwargs change.
authorBen Darnell <ben@bendarnell.com>
Sat, 18 Apr 2015 22:53:38 +0000 (18:53 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 18 Apr 2015 22:53:38 +0000 (18:53 -0400)
tornado/concurrent.py
tornado/test/concurrent_test.py

index d4bfaa9c425e962e7a0acfbed7d29a078cafaa98..51ae239fb8a53967363910d0781c896ab19ad236 100644 (file)
@@ -343,8 +343,16 @@ def run_on_executor(*args, **kwargs):
     The decorated method may be called with a ``callback`` keyword
     argument and returns a future.
 
-    This decorator should be used only on methods of objects with attributes
-    ``executor`` and ``io_loop``.
+    The `.IOLoop` and executor to be used are determined by the ``io_loop``
+    and ``executor`` attributes of ``self``. To use different attributes,
+    pass keyword arguments to the decorator::
+
+        @run_on_executor(executor='_thread_pool')
+        def foo(self):
+            pass
+
+    .. versionchanged:: 4.2
+       Added keyword arguments to use alternative attributes.
     """
     def run_on_executor_decorator(fn):
         executor = kwargs.get("executor", "executor")
@@ -354,12 +362,16 @@ def run_on_executor(*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()))
+                getattr(self, io_loop).add_future(
+                    future, lambda future: callback(future.result()))
             return future
         return wrapper
-    if len(args) == 1 and callable(args[0]):
+    if args and kwargs:
+        raise ValueError("cannot combine positional and keyword args")
+    if len(args) == 1:
         return run_on_executor_decorator(args[0])
+    elif len(args) != 0:
+        raise ValueError("expected 1 argument, got %d", len(args))
     return run_on_executor_decorator
 
 
index 98b13a11c9aa4448c248c5ab959eaf5dc52f2a8e..bf90ad0ec92cd829f0084941258dc239ebbc54c5 100644 (file)
@@ -351,8 +351,8 @@ class RunOnExecutorTest(AsyncTestCase):
                 return 42
 
         o = Object(io_loop=self.io_loop)
-        anwser = yield o.f()
-        self.assertEqual(anwser, 42)
+        answer = yield o.f()
+        self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_no_args(self):
@@ -366,8 +366,8 @@ class RunOnExecutorTest(AsyncTestCase):
                 return 42
 
         o = Object(io_loop=self.io_loop)
-        anwser = yield o.f()
-        self.assertEqual(anwser, 42)
+        answer = yield o.f()
+        self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_io_loop(self):
@@ -381,8 +381,8 @@ class RunOnExecutorTest(AsyncTestCase):
                 return 42
 
         o = Object(io_loop=self.io_loop)
-        anwser = yield o.f()
-        self.assertEqual(anwser, 42)
+        answer = yield o.f()
+        self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_executor(self):
@@ -396,8 +396,8 @@ class RunOnExecutorTest(AsyncTestCase):
                 return 42
 
         o = Object(io_loop=self.io_loop)
-        anwser = yield o.f()
-        self.assertEqual(anwser, 42)
+        answer = yield o.f()
+        self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_both(self):
@@ -411,5 +411,5 @@ class RunOnExecutorTest(AsyncTestCase):
                 return 42
 
         o = Object(io_loop=self.io_loop)
-        anwser = yield o.f()
-        self.assertEqual(anwser, 42)
+        answer = yield o.f()
+        self.assertEqual(answer, 42)