In this lesson, you will learn how to leverage TaskGroup to enforce structured concurrency, ensuring that the entire lifecycle of your asynchronous tasks is managed automatically and safely.
The Shift from gather to TaskGroup
While asyncio.gather() is a common way to run tasks concurrently, it has a significant drawback: if one task fails, the others continue to run in the background unless you manually track and cancel them. This often leads to "leaked" tasks and complex cleanup logic.
TaskGroup solves this by introducing a context manager that dictates the lifecycle of all tasks spawned within its block. When you use an async with asyncio.TaskGroup() as tg: block, the Python event loop ensures that all tasks are awaited before exiting the block. More importantly, if any task within the group fails, the TaskGroup automatically cancels all other pending tasks in the group and waits for them to shut down gracefully before propagating the exception.
Practical Implementation
Consider a scenario where you need to perform multiple API calls simultaneously. If one fails, you want to ensure the others are aborted immediately to save resources:
import asyncio
async def fetch_data(name, delay):
await asyncio.sleep(delay)
if name == "Service-B":
raise ValueError("Service-B failed!")
print(f"{name} completed.")
async def main():
try:
async with asyncio.TaskGroup() as tg:
tg.create_task(fetch_data("Service-A", 1))
tg.create_task(fetch_data("Service-B", 0.5))
tg.create_task(fetch_data("Service-C", 2))
except ExceptionGroup as eg:
print(f"Caught errors: {eg.exceptions}")
asyncio.run(main())
In this code, as soon as Service-B raises an exception, the TaskGroup triggers a cancellation for Service-A and Service-C. You no longer need to write manual try/except blocks or handle Task.cancel() calls for every individual operation. The lifecycle of your concurrency is now bound strictly to the async with block, making your code cleaner and significantly more resilient to partial failures.
Key Takeaway: TaskGroups provide structured concurrency by automatically managing the lifecycle, cancellation, and synchronization of grouped tasks, ensuring that no orphaned coroutines persist when a failure occurs.