Skip to content

Commit d42528a

Browse files
authored
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
1 parent 7f03445 commit d42528a

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
@@ -387,6 +387,14 @@ Running Tasks Concurrently
387387
# Task C: Compute factorial(4)...
388388
# Task C: factorial(4) = 24
389389

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

Lib/asyncio/tasks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,13 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
729729
the outer Future is *not* cancelled in this case. (This is to
730730
prevent the cancellation of one child to cause other children to
731731
be cancelled.)
732+
733+
If *return_exceptions* is False, cancelling gather() after it
734+
has been marked done won't cancel any submitted awaitables.
735+
For instance, gather can be marked done after propagating an
736+
exception to the caller, therefore, calling ``gather.cancel()``
737+
after catching an exception (raised by one of the awaitables) from
738+
gather won't cancel any other awaitables.
732739
"""
733740
if not coros_or_futures:
734741
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)