Skip to content

Commit

Permalink
bpo-37703: improve asyncio.gather documentation regarding cancellation (
Browse files Browse the repository at this point in the history
GH-15312)

These changes updates the doc to comprehensively mention the behaviour of gather.cancel()

Automerge-Triggered-By: @asvetlov
(cherry picked from commit d42528a)

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>
  • Loading branch information
miss-islington and vinay0410 committed Jul 20, 2020
1 parent 2fbb0d8 commit 46634b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Doc/library/asyncio-task.rst
Expand Up @@ -387,6 +387,14 @@ Running Tasks Concurrently
# Task C: Compute factorial(4)...
# Task C: factorial(4) = 24

.. note::
If *return_exceptions* is False, cancelling gather() after it
has been marked done won't cancel any submitted awaitables.
For instance, gather can be marked done after propagating an
exception to the caller, therefore, calling ``gather.cancel()``
after catching an exception (raised by one of the awaitables) from
gather won't cancel any other awaitables.

.. versionchanged:: 3.7
If the *gather* itself is cancelled, the cancellation is
propagated regardless of *return_exceptions*.
Expand Down
7 changes: 7 additions & 0 deletions Lib/asyncio/tasks.py
Expand Up @@ -729,6 +729,13 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
the outer Future is *not* cancelled in this case. (This is to
prevent the cancellation of one child to cause other children to
be cancelled.)
If *return_exceptions* is False, cancelling gather() after it
has been marked done won't cancel any submitted awaitables.
For instance, gather can be marked done after propagating an
exception to the caller, therefore, calling ``gather.cancel()``
after catching an exception (raised by one of the awaitables) from
gather won't cancel any other awaitables.
"""
if not coros_or_futures:
if loop is None:
Expand Down
@@ -0,0 +1,2 @@
Updated Documentation to comprehensively elaborate on the behaviour of
gather.cancel()

0 comments on commit 46634b7

Please sign in to comment.