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")
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
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):
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):
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):
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):
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)