Task groups and cancellation
When two operations belong to one job, what should happen if one fails? Run this example as a local script with Python 3.11 or later, after studying async and context managers.
On this page
Read the model
A task group owns the tasks started through it. Leaving its context waits for them. If a child raises an ordinary exception, the group cancels unfinished siblings, waits for their cleanup, then reports failures together. Cancellation reaches a task at a point where it can suspend.
Follow the example
import asyncio
async def worker(ready):
try:
ready.set()
await asyncio.Event().wait()
finally:
print("closed")
async def reader(ready):
await ready.wait()
raise ValueError("invalid record")
async def main():
ready = asyncio.Event()
try:
async with asyncio.TaskGroup() as group:
group.create_task(worker(ready))
group.create_task(reader(ready))
except* ValueError:
print("batch failed")
asyncio.run(main())The event ensures the worker is waiting before its sibling fails. Cancellation interrupts the wait. The finally block prints closed before the group exits. The outer handler then prints batch failed. No sleep duration or scheduling guess is needed to make this demonstration work.
Change one condition
Move the cleanup after the wait and outside finally. Predict which line disappears when the sibling fails. The wait raises, so execution skips the next ordinary statement. Restoring finally handles both normal completion and cancellation. Cleanup cannot undo an external side effect that already completed.