diff options
author | Adrien <mrnycticorax@gmail.com> | 2023-04-20 14:07:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-20 14:07:41 (GMT) |
commit | 4898415df724380d4c8b0ec08e8cb92f126193c3 (patch) | |
tree | 899fa5fbaee4d858cdef13960415a48fc2f6ee4d | |
parent | 6be7aee18c5b8e639103df951d0d277f4b46f902 (diff) | |
download | cpython-4898415df724380d4c8b0ec08e8cb92f126193c3.zip cpython-4898415df724380d4c8b0ec08e8cb92f126193c3.tar.gz cpython-4898415df724380d4c8b0ec08e8cb92f126193c3.tar.bz2 |
gh-98641: Document difference between task group and gather (#103644)
The purpose of the comments is to rule out the implication that asyncio.TaskGroup is a drop-in replacement / better alternative to asyncio.gather().
-rw-r--r-- | Doc/library/asyncio-task.rst | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index b81d89a..ba0f909 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -256,8 +256,9 @@ Creating Tasks .. note:: - :meth:`asyncio.TaskGroup.create_task` is a newer alternative - that allows for convenient waiting for a group of related tasks. + :meth:`asyncio.TaskGroup.create_task` is a new alternative + leveraging structural concurrency; it allows for waiting + for a group of related tasks with strong safety guarantees. .. important:: @@ -340,7 +341,7 @@ Example:: async with asyncio.TaskGroup() as tg: task1 = tg.create_task(some_coro(...)) task2 = tg.create_task(another_coro(...)) - print("Both tasks have completed now.") + print(f"Both tasks have completed now: {task1.result()}, {task2.result()}") 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 @@ -459,8 +460,12 @@ Running Tasks Concurrently Tasks/Futures to be cancelled. .. note:: - A more modern way to create and run tasks concurrently and - wait for their completion is :class:`asyncio.TaskGroup`. + A new alternative to create and run tasks concurrently and + wait for their completion is :class:`asyncio.TaskGroup`. *TaskGroup* + provides stronger safety guarantees than *gather* for scheduling a nesting of subtasks: + if a task (or a subtask, a task scheduled by a task) + raises an exception, *TaskGroup* will, while *gather* will not, + cancel the remaining scheduled tasks). .. _asyncio_example_gather: |