Skip to content

Commit 58f59a9

Browse files
bpo-37703: improve asyncio.gather documentation regarding cancellation (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>
1 parent 892fc8d commit 58f59a9

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Doc/library/asyncio-task.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ Running Tasks Concurrently
385385
# Task C: Compute factorial(4)...
386386
# Task C: factorial(4) = 24
387387

388+
.. note::
389+
If *return_exceptions* is False, cancelling gather() after it
390+
has been marked done won't cancel any submitted awaitables.
391+
For instance, gather can be marked done after propagating an
392+
exception to the caller, therefore, calling ``gather.cancel()``
393+
after catching an exception (raised by one of the awaitables) from
394+
gather won't cancel any other awaitables.
395+
388396
.. versionchanged:: 3.7
389397
If the *gather* itself is cancelled, the cancellation is
390398
propagated regardless of *return_exceptions*.

Lib/asyncio/tasks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,13 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
736736
the outer Future is *not* cancelled in this case. (This is to
737737
prevent the cancellation of one child to cause other children to
738738
be cancelled.)
739+
740+
If *return_exceptions* is False, cancelling gather() after it
741+
has been marked done won't cancel any submitted awaitables.
742+
For instance, gather can be marked done after propagating an
743+
exception to the caller, therefore, calling ``gather.cancel()``
744+
after catching an exception (raised by one of the awaitables) from
745+
gather won't cancel any other awaitables.
739746
"""
740747
if not coros_or_futures:
741748
if loop is None:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated Documentation to comprehensively elaborate on the behaviour of
2+
gather.cancel()

0 commit comments

Comments
 (0)