diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-05-28 17:16:43 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-28 17:16:43 -0700 |
| commit | ddc613f20394eba25f2420f6d2fa62b277c95058 (patch) | |
| tree | dc7e7d6f619af474152207cd537f46b3f82d76dd /Lib/asyncio | |
| parent | 8d8b86116fae91570c26fa48974b54986fbd1b72 (diff) | |
| download | cpython-git-ddc613f20394eba25f2420f6d2fa62b277c95058.tar.gz | |
bpo-32610: Fix asyncio.all_tasks() to return only pending tasks. (GH-7174)
(cherry picked from commit 416c1ebd9896b394790dcb4f9f035b1a44ebe9ff)
Co-authored-by: Yury Selivanov <yury@magic.io>
Diffstat (limited to 'Lib/asyncio')
| -rw-r--r-- | Lib/asyncio/__init__.py | 4 | ||||
| -rw-r--r-- | Lib/asyncio/runners.py | 3 | ||||
| -rw-r--r-- | Lib/asyncio/tasks.py | 12 |
3 files changed, 16 insertions, 3 deletions
diff --git a/Lib/asyncio/__init__.py b/Lib/asyncio/__init__.py index 23ea055912..2685902430 100644 --- a/Lib/asyncio/__init__.py +++ b/Lib/asyncio/__init__.py @@ -18,6 +18,10 @@ from .subprocess import * from .tasks import * from .transports import * +# Exposed for _asynciomodule.c to implement now deprecated +# Task.all_tasks() method. This function will be removed in 3.9. +from .tasks import _all_tasks_compat # NoQA + __all__ = (base_events.__all__ + coroutines.__all__ + events.__all__ + diff --git a/Lib/asyncio/runners.py b/Lib/asyncio/runners.py index bb54b72527..5fbab03dd0 100644 --- a/Lib/asyncio/runners.py +++ b/Lib/asyncio/runners.py @@ -51,8 +51,7 @@ def run(main, *, debug=False): def _cancel_all_tasks(loop): - to_cancel = [task for task in tasks.all_tasks(loop) - if not task.done()] + to_cancel = tasks.all_tasks(loop) if not to_cancel: return diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 4a9db2a3a0..67fb57c6a7 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -34,6 +34,16 @@ def current_task(loop=None): def all_tasks(loop=None): """Return a set of all tasks for the loop.""" if loop is None: + loop = events.get_running_loop() + return {t for t in _all_tasks + if futures._get_loop(t) is loop and not t.done()} + + +def _all_tasks_compat(loop=None): + # Different from "all_task()" by returning *all* Tasks, including + # the completed ones. Used to implement deprecated "Tasks.all_task()" + # method. + if loop is None: loop = events.get_event_loop() return {t for t in _all_tasks if futures._get_loop(t) is loop} @@ -82,7 +92,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation "use asyncio.all_tasks() instead", PendingDeprecationWarning, stacklevel=2) - return all_tasks(loop) + return _all_tasks_compat(loop) def __init__(self, coro, *, loop=None): super().__init__(loop=loop) |
