]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
concurrent: Remove use of self.io_loop from run_on_executor 2052/head
authorBen Darnell <ben@bendarnell.com>
Mon, 22 May 2017 05:03:46 +0000 (01:03 -0400)
committerBen Darnell <ben@bendarnell.com>
Mon, 22 May 2017 05:03:46 +0000 (01:03 -0400)
tornado/concurrent.py
tornado/test/concurrent_test.py

index 667e6b1788ecfe426e1f02b084d01840fd1929a3..b1bcc56e2cf9a06c72610198f4e6b8c93838a14c 100644 (file)
@@ -378,9 +378,9 @@ def run_on_executor(*args, **kwargs):
     The decorated method may be called with a ``callback`` keyword
     argument and returns a future.
 
-    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::
+    The executor to be used is determined by the ``executor``
+    attributes of ``self``. To use a different attribute name, pass a
+    keyword argument to the decorator::
 
         @run_on_executor(executor='_thread_pool')
         def foo(self):
@@ -388,17 +388,20 @@ def run_on_executor(*args, **kwargs):
 
     .. versionchanged:: 4.2
        Added keyword arguments to use alternative attributes.
+
+    .. versionchanged:: 5.0
+       Always uses the current IOLoop instead of ``self.io_loop``.
     """
     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(
+                from tornado.ioloop import IOLoop
+                IOLoop.current().add_future(
                     future, lambda future: callback(future.result()))
             return future
         return wrapper
index e20281fc34745a71b386b8dc76088c07198f5f61..d1e266ff05a34afe2d6b7565c84f8ddb9689ce6b 100644 (file)
@@ -362,74 +362,41 @@ class RunOnExecutorTest(AsyncTestCase):
     @gen_test
     def test_no_calling(self):
         class Object(object):
-            def __init__(self, io_loop):
-                self.io_loop = io_loop
+            def __init__(self):
                 self.executor = futures.thread.ThreadPoolExecutor(1)
 
             @run_on_executor
             def f(self):
                 return 42
 
-        o = Object(io_loop=self.io_loop)
+        o = Object()
         answer = yield o.f()
         self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_no_args(self):
         class Object(object):
-            def __init__(self, io_loop):
-                self.io_loop = io_loop
+            def __init__(self):
                 self.executor = futures.thread.ThreadPoolExecutor(1)
 
             @run_on_executor()
             def f(self):
                 return 42
 
-        o = Object(io_loop=self.io_loop)
-        answer = yield o.f()
-        self.assertEqual(answer, 42)
-
-    @gen_test
-    def test_call_with_io_loop(self):
-        class Object(object):
-            def __init__(self, io_loop):
-                self._io_loop = io_loop
-                self.executor = futures.thread.ThreadPoolExecutor(1)
-
-            @run_on_executor(io_loop='_io_loop')
-            def f(self):
-                return 42
-
-        o = Object(io_loop=self.io_loop)
+        o = Object()
         answer = yield o.f()
         self.assertEqual(answer, 42)
 
     @gen_test
     def test_call_with_executor(self):
         class Object(object):
-            def __init__(self, io_loop):
-                self.io_loop = io_loop
+            def __init__(self):
                 self.__executor = futures.thread.ThreadPoolExecutor(1)
 
             @run_on_executor(executor='_Object__executor')
             def f(self):
                 return 42
 
-        o = Object(io_loop=self.io_loop)
-        answer = yield o.f()
-        self.assertEqual(answer, 42)
-
-    @gen_test
-    def test_call_with_both(self):
-        class Object(object):
-            def __init__(self, io_loop):
-                self._io_loop = io_loop
-                self.__executor = futures.thread.ThreadPoolExecutor(1)
-
-            @run_on_executor(io_loop='_io_loop', executor='_Object__executor')
-            def f(self):
-                return 42
-
-        o = Object(io_loop=self.io_loop)
+        o = Object()
         answer = yield o.f()
         self.assertEqual(answer, 42)