Asyncio TaskGroup Timeout Gather
Fan out concurrent work with asyncio TaskGroup, wrapping the group in wait_for so one timeout cancels every straggler cleanly.
Code
import asyncio
async def fetch_shard(shard: int) -> list[int]:
await asyncio.sleep(0.2 * shard) # 各分片耗时不一
return [shard * 10, shard * 10 + 1]
async def fetch_slow_shard(shard: int) -> list[int]:
await asyncio.sleep(10)
return []
async def gather_with_timeout(coros, timeout: float) -> list[int]:
async def runner():
results: list[int] = []
async with asyncio.TaskGroup() as tg:
tasks = [tg.create_task(c) for c in coros]
results = [item for task in tasks for item in task.result()]
return results
try:
return await asyncio.wait_for(runner(), timeout=timeout)
except TimeoutError:
print('deadline hit, all pending tasks cancelled')
return []
async def main() -> None:
fast = await gather_with_timeout(
[fetch_shard(i) for i in range(4)], timeout=2.0
)
print('fast path collected:', fast)
interrupted = await gather_with_timeout(
[fetch_shard(1), fetch_slow_shard(9)], timeout=1.0
)
print('timeout path result:', interrupted)
asyncio.run(main())How it works
Asyncio TaskGroup Timeout Gather comes down to the functions fetch_shard, fetch_slow_shard, gather_with_timeout and runner.
Asynchronous boundaries use await, so failures surface as exceptions at the call site rather than being swallowed.
The return value is newly constructed — inputs are never handed back.
What to watch for
- Requires
asyncioas a package; the snippet will not run without it.
Applying it
The main signature reads def fetch_shard(shard: int) -> list[int]
The demo output is labeled (deadline hit, all pending tasks cancelled, fast path collected:, timeout path result:), so every printed line maps back to its originating operation.
fetch_shard is the public entry point; everything else in the file exists to support it.
Statements that matter
async def fetch_slow_shard(shard: int) -> list[int]:
await asyncio.sleep(10)
return []
async def fetch_shard(shard: int) -> list[int]:
await asyncio.sleep(0.2 * shard) # 各分片耗时不一
return [shard * 10, shard * 10 + 1]
interrupted = await gather_with_timeout(
[fetch_shard(1), fetch_slow_shard(9)], timeout=1.0
)
Interactive Tools for This Topic
FAQ
- Does `fetch_shard` modify its input?
- No mutation happens — the function reads its input and returns a new value.
- Are there third-party dependencies?
- `asyncio` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.