From f5ad26b2b4772ed8598667980aa3c5870cc796a0 Mon Sep 17 00:00:00 2001 From: Vinay Sharma Date: Fri, 16 Aug 2019 20:26:29 +0530 Subject: [PATCH] bpo-37704: improve asyncio.gather documentation regarding cancellation --- Doc/library/asyncio-task.rst | 8 ++++++++ Lib/asyncio/tasks.py | 7 +++++++ .../2019-08-16-20-25-42.bpo-37703.Qm_l_H.rst | 2 ++ 3 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Documentation/2019-08-16-20-25-42.bpo-37703.Qm_l_H.rst diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 1fcdcb985d88424..5d15f4832b70bed 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -370,6 +370,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*. diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index cd4832cfee246bb..aef34fe477ef1f9 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -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: diff --git a/Misc/NEWS.d/next/Documentation/2019-08-16-20-25-42.bpo-37703.Qm_l_H.rst b/Misc/NEWS.d/next/Documentation/2019-08-16-20-25-42.bpo-37703.Qm_l_H.rst new file mode 100644 index 000000000000000..a1a1c354b1688b0 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-08-16-20-25-42.bpo-37703.Qm_l_H.rst @@ -0,0 +1,2 @@ +Updated Documentation to comprehensively elaborate on the behaviour of +gather.cancel()