https://github.com/python/cpython/commit/8a8d737be200aef4d3f4ffd576505d2ff660a3a8 commit: 8a8d737be200aef4d3f4ffd576505d2ff660a3a8 branch: main author: John Belmonte <[email protected]> committer: gvanrossum <[email protected]> date: 2026-04-28T09:45:55-07:00 summary:
gh-108951: document addition of TaskGroup.cancel() (#149031) Co-authored-by: Alex Grönholm <[email protected]> Co-authored-by: Bénédikt Tran <[email protected]> files: M Doc/library/asyncio-task.rst M Doc/whatsnew/3.15.rst M Misc/NEWS.d/next/Library/2024-11-24-07-18-40.gh-issue-108951.jyKygP.rst 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 6f0f7021de8e7a..eb08f8c4ed69e7 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -741,6 +741,17 @@ ast (Contributed by Stan Ulbrych in :gh:`148981`.) +asyncio +------- + +* Added :meth:`TaskGroup.cancel <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 ------ 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..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 error. +:mod:`asyncio`: Add :meth:`TaskGroup.cancel <asyncio.TaskGroup.cancel>` which cancels +unfinished tasks and exits the group without raising :exc:`asyncio.CancelledError`. _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
