diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-05-28 11:42:50 -0700 |
|---|---|---|
| committer | Yury Selivanov <yury@magic.io> | 2018-05-28 14:42:50 -0400 |
| commit | f8fdb368e3d8d048bffc248fbe1023651c276671 (patch) | |
| tree | 87feaf2dcc89ab42e12d05a972240bf979cb315b | |
| parent | 1f21ae710d83a37c872355612b58958cef4d5f95 (diff) | |
| download | cpython-git-f8fdb368e3d8d048bffc248fbe1023651c276671.tar.gz | |
bpo-33505: Optimize asyncio.ensure_future by reordering if conditions (GH-6836) (#7162)
(cherry picked from commit e549c4be5fb010f5faf12236af8faa720a1429be)
Co-authored-by: jimmylai <albert_chs@yahoo.com.tw>
| -rw-r--r-- | Lib/asyncio/tasks.py | 10 | ||||
| -rw-r--r-- | Misc/NEWS.d/next/Library/2018-05-14-18-05-35.bpo-33505.L8pAyt.rst | 1 |
2 files changed, 6 insertions, 5 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 5df1af6642..4a9db2a3a0 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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 index 0000000000..201b02781c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-05-14-18-05-35.bpo-33505.L8pAyt.rst @@ -0,0 +1 @@ +Optimize asyncio.ensure_future() by reordering if checks: 1.17x faster. |
