Skip to content

Commit f8fdb36

Browse files
miss-islingtonjimmylai
authored andcommitted
bpo-33505: Optimize asyncio.ensure_future by reordering if conditions (GH-6836) (#7162)
(cherry picked from commit e549c4b) Co-authored-by: jimmylai <[email protected]>
1 parent 1f21ae7 commit f8fdb36

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Lib/asyncio/tasks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,17 +542,17 @@ def ensure_future(coro_or_future, *, loop=None):
542542
543543
If the argument is a Future, it is returned directly.
544544
"""
545-
if futures.isfuture(coro_or_future):
546-
if loop is not None and loop is not futures._get_loop(coro_or_future):
547-
raise ValueError('loop argument must agree with Future')
548-
return coro_or_future
549-
elif coroutines.iscoroutine(coro_or_future):
545+
if coroutines.iscoroutine(coro_or_future):
550546
if loop is None:
551547
loop = events.get_event_loop()
552548
task = loop.create_task(coro_or_future)
553549
if task._source_traceback:
554550
del task._source_traceback[-1]
555551
return task
552+
elif futures.isfuture(coro_or_future):
553+
if loop is not None and loop is not futures._get_loop(coro_or_future):
554+
raise ValueError('loop argument must agree with Future')
555+
return coro_or_future
556556
elif inspect.isawaitable(coro_or_future):
557557
return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
558558
else:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize asyncio.ensure_future() by reordering if checks: 1.17x faster.

0 commit comments

Comments
 (0)