liveBook · Manning

Pending set

As explained in Python Concurrency with asyncio

A pending set is the set returned by asyncio.wait containing awaitables that have not completed when the wait returns.

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

How Python Concurrency with asyncio covers it

How deep it goes
Explains membership, FIRST_EXCEPTION behavior, timeout behavior, and explicit cancellation, without descending into task internals.
Examples
Uses concurrent web requests, failed requests, delayed requests, timeout output, and done-task result handling.
What it assumes
Assumes familiarity with asynchronous tasks, awaitables, exceptions, and concurrent web requests.
What you can do after
Operate asyncio.wait by inspecting pending tasks and explicitly canceling them after exceptions or timeouts.
How it connects
Connects pending tasks with asyncio.wait, task cancellation, exceptions, timeouts, asyncio.gather, wait_for, and as_completed.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.