Asyncio Taskgroup With Timeout
Use asyncio.TaskGroup with timeout for structured concurrency and automatic cancellation on failure.
Code
import asyncio
async def run_with_timeout(tasks, timeout=30):
async with asyncio.timeout(timeout):
async with asyncio.TaskGroup() as tg:
results = []
for task in tasks:
t = tg.create_task(task())
results.append(t)
return [t.result() for t in results]Report a problem with this exampleOpens a pre-filled issue on GitHub.