]> granicus.if.org Git - python/commitdiff
bpo-33505: Optimize asyncio.ensure_future by reordering if conditions (GH-6836)
authorjimmylai <albert_chs@yahoo.com.tw>
Mon, 28 May 2018 16:42:05 +0000 (06:42 -1000)
committerYury Selivanov <yury@magic.io>
Mon, 28 May 2018 16:42:05 +0000 (12:42 -0400)
Lib/asyncio/tasks.py
Misc/NEWS.d/next/Library/2018-05-14-18-05-35.bpo-33505.L8pAyt.rst [new file with mode: 0644]

index 5df1af6642e49e2fab5648fce1336461a90af9a9..4a9db2a3a05ceb13b511958b7f760f495dbdbb77 100644 (file)
@@ -542,17 +542,17 @@ def ensure_future(coro_or_future, *, loop=None):
 
     If the argument is a Future, it is returned directly.
     """
-    if futures.isfuture(coro_or_future):
-        if loop is not None and loop is not futures._get_loop(coro_or_future):
-            raise ValueError('loop argument must agree with Future')
-        return coro_or_future
-    elif coroutines.iscoroutine(coro_or_future):
+    if coroutines.iscoroutine(coro_or_future):
         if loop is None:
             loop = events.get_event_loop()
         task = loop.create_task(coro_or_future)
         if task._source_traceback:
             del task._source_traceback[-1]
         return task
+    elif futures.isfuture(coro_or_future):
+        if loop is not None and loop is not futures._get_loop(coro_or_future):
+            raise ValueError('loop argument must agree with Future')
+        return coro_or_future
     elif inspect.isawaitable(coro_or_future):
         return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
     else:
diff --git a/Misc/NEWS.d/next/Library/2018-05-14-18-05-35.bpo-33505.L8pAyt.rst b/Misc/NEWS.d/next/Library/2018-05-14-18-05-35.bpo-33505.L8pAyt.rst
new file mode 100644 (file)
index 0000000..201b027
--- /dev/null
@@ -0,0 +1 @@
+Optimize asyncio.ensure_future() by reordering if checks: 1.17x faster.