From 2bf9185fe44f5231710a1b8a4ae02af7136e31d7 Mon Sep 17 00:00:00 2001 From: John Belmonte Date: Sun, 26 Apr 2026 15:56:28 -0700 Subject: [PATCH 1/5] gh-108951: Whatsnew and doc tweaks for TaskGroup.cancel() --- Doc/library/asyncio-task.rst | 4 ++-- Doc/whatsnew/3.15.rst | 7 +++++++ .../Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index f0fe91b363d95e..2e17d0dc70c7a5 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -394,8 +394,8 @@ Example:: The ``async with`` statement will wait for all tasks in the group to finish. While waiting, new tasks may still be added to the group (for example, by passing ``tg`` into one of the coroutines -and calling ``tg.create_task()`` in that coroutine). There is also opportunity -to short-circuit the entire task group with ``tg.cancel()``, based on some condition. +and calling ``tg.create_task()`` in that coroutine). There is also opportunity to +request termination of the entire task group with ``tg.cancel()``, based on some condition. Once the last task has finished and the ``async with`` block is exited, no new tasks may be added to the group. diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 405d388af487e8..c7f1918ae1ad32 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -717,6 +717,13 @@ ast This can be :ref:`controlled using environment variables `. (Contributed by Stan Ulbrych in :gh:`148981`.) +asyncio +------- +* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group -- + e.g. when the goal of the tasks has been achieved or their services no longer needed. + Previously this would involve unintuitive boilerplate such as an extra task raising + a custom exception which is then suppressed as it exits TaskGroup. + (Contributed by John Belmonte in :gh:`127214`.) base64 ------ diff --git a/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst b/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst index 1696a2dd1728ed..bad8ab66e24c2f 100644 --- a/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst +++ b/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst @@ -1 +1 @@ -Add :meth:`~asyncio.TaskGroup.cancel` which cancels unfinished tasks and exits the group without error. +Add :meth:`~asyncio.TaskGroup.cancel` which cancels unfinished tasks and exits the group without raising :exc:`asyncio.CancelledError`. From e6a42b92af065cc9e07a09ae9aee49500014990c Mon Sep 17 00:00:00 2001 From: John Belmonte Date: Sun, 26 Apr 2026 17:21:24 -0700 Subject: [PATCH 2/5] formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alex Grönholm --- Doc/whatsnew/3.15.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index c7f1918ae1ad32..af02b2bb258df0 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -719,6 +719,7 @@ ast asyncio ------- + * Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group -- e.g. when the goal of the tasks has been achieved or their services no longer needed. Previously this would involve unintuitive boilerplate such as an extra task raising From ccdc58e20151e1ccb186ffe66b84d807fd070924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 28 Apr 2026 09:44:11 +0200 Subject: [PATCH 3/5] Update Doc/whatsnew/3.15.rst --- Doc/whatsnew/3.15.rst | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index af02b2bb258df0..7d466d31799fcc 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -717,15 +717,18 @@ ast This can be :ref:`controlled using environment variables `. (Contributed by Stan Ulbrych in :gh:`148981`.) + asyncio ------- -* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group -- - e.g. when the goal of the tasks has been achieved or their services no longer needed. - Previously this would involve unintuitive boilerplate such as an extra task raising - a custom exception which is then suppressed as it exits TaskGroup. +* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group, + for instance, when the goal of the tasks has been achieved or their services + are no longer needed. Previously this would involve unintuitive boilerplate such + as an extra task raising a custom exception which is then suppressed as it + exits the task group. (Contributed by John Belmonte in :gh:`127214`.) + base64 ------ From c67dbab0a96b4d07f13ff54bc36687993890eb68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 28 Apr 2026 09:48:59 +0200 Subject: [PATCH 4/5] Wrap lines under 80 characters. --- Doc/whatsnew/3.15.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index 7d466d31799fcc..4594a3d3b515ff 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -721,11 +721,11 @@ ast asyncio ------- -* Added :meth:`~asyncio.TaskGroup.cancel` to allow early termination of a task group, - for instance, when the goal of the tasks has been achieved or their services - are no longer needed. Previously this would involve unintuitive boilerplate such - as an extra task raising a custom exception which is then suppressed as it - exits the task group. +* Added :meth:`TaskGroup.cancel ` to allow early + termination of a task group, for instance, when the goal of the tasks has + been achieved or their services are no longer needed. + Previously this would involve unintuitive boilerplate such as an extra task + raising a custom exception which is then suppressed as it exits the task group. (Contributed by John Belmonte in :gh:`127214`.) From 305ad786821858bcaa04be56f3f77c851de79337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 28 Apr 2026 09:50:11 +0200 Subject: [PATCH 5/5] Use `TaskGroup.cancel` in NEWS entry for the link --- .../Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst b/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst index bad8ab66e24c2f..0e0280c9b6b0e7 100644 --- a/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst +++ b/Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst @@ -1 +1,2 @@ -Add :meth:`~asyncio.TaskGroup.cancel` which cancels unfinished tasks and exits the group without raising :exc:`asyncio.CancelledError`. +:mod:`asyncio`: Add :meth:`TaskGroup.cancel ` which cancels +unfinished tasks and exits the group without raising :exc:`asyncio.CancelledError`.