A pending set is the set returned by asyncio.wait containing awaitables that have not completed when the wait returns. The done set contains tasks that completed successfully or with exceptions. section 4.6.1
Use the pending set to decide what should happen to unfinished tasks:
- With the default ALL_COMPLETED behavior, asyncio.wait returns only after every task finishes, so the pending set is empty. section 4.6.1
- With FIRST_EXCEPTION, the set may contain tasks still running after another task fails; loop over it and call cancel when those tasks should stop. section 4.6.2
- With a timeout, wait returns completed and pending tasks without raising a timeout error or canceling the pending tasks automatically. section 4.6.4
Canceling unfinished requests
Start one failing request and two delayed requests with asyncio.wait(..., return_when=asyncio.FIRST_EXCEPTION). The failed request appears in done, while the delayed requests appear in pending and can be canceled explicitly. section 4.6.2
Listing 4.15 Using timeouts with wait
@async_timed()
async def main():
async with aiohttp.ClientSession() as session:
url = 'https:/ / example .com'
fetchers = [asyncio.create_task(fetch_status(session, url),
asyncio.create_task(fetch_status(session, url),
asyncio.create_task(fetch_status(session, url, delay=3))]
done, pending = await asyncio.wait(fetchers, timeout=1)
print(f'Done task count: {len(done)}')
print(f'Pending task count: {len(pending)}')
for done_task in done:
result = await done_task
print(result)
asyncio.run(main())
Inspect a timeout example where two fast requests enter done and a slower request remains pending after one second. section 4.6.4
