Skip to content

Commit 4898415

Browse files
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().
1 parent 6be7aee commit 4898415

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Doc/library/asyncio-task.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,9 @@ Creating Tasks
256256

257257
.. note::
258258

259-
:meth:`asyncio.TaskGroup.create_task` is a newer alternative
260-
that allows for convenient waiting for a group of related tasks.
259+
:meth:`asyncio.TaskGroup.create_task` is a new alternative
260+
leveraging structural concurrency; it allows for waiting
261+
for a group of related tasks with strong safety guarantees.
261262

262263
.. important::
263264

@@ -340,7 +341,7 @@ Example::
340341
async with asyncio.TaskGroup() as tg:
341342
task1 = tg.create_task(some_coro(...))
342343
task2 = tg.create_task(another_coro(...))
343-
print("Both tasks have completed now.")
344+
print(f"Both tasks have completed now: {task1.result()}, {task2.result()}")
344345

345346
The ``async with`` statement will wait for all tasks in the group to finish.
346347
While waiting, new tasks may still be added to the group
@@ -459,8 +460,12 @@ Running Tasks Concurrently
459460
Tasks/Futures to be cancelled.
460461

461462
.. note::
462-
A more modern way to create and run tasks concurrently and
463-
wait for their completion is :class:`asyncio.TaskGroup`.
463+
A new alternative to create and run tasks concurrently and
464+
wait for their completion is :class:`asyncio.TaskGroup`. *TaskGroup*
465+
provides stronger safety guarantees than *gather* for scheduling a nesting of subtasks:
466+
if a task (or a subtask, a task scheduled by a task)
467+
raises an exception, *TaskGroup* will, while *gather* will not,
468+
cancel the remaining scheduled tasks).
464469

465470
.. _asyncio_example_gather:
466471

0 commit comments

Comments
 (0)