From: Ben Darnell Date: Sat, 18 Apr 2015 22:53:38 +0000 (-0400) Subject: Clean up and document run_on_executor kwargs change. X-Git-Tag: v4.2.0b1~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b450ff270df0395ee80f4ee5896a92bfe7f9b6ae;p=thirdparty%2Ftornado.git Clean up and document run_on_executor kwargs change. --- diff --git a/tornado/concurrent.py b/tornado/concurrent.py index d4bfaa9c4..51ae239fb 100644 --- a/tornado/concurrent.py +++ b/tornado/concurrent.py @@ -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 diff --git a/tornado/test/concurrent_test.py b/tornado/test/concurrent_test.py index 98b13a11c..bf90ad0ec 100644 --- a/tornado/test/concurrent_test.py +++ b/tornado/test/concurrent_test.py @@ -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)