liveBook · Manning

Asyncio.gather

As explained in Python Concurrency with asyncio

asyncio.gather is an asyncio function that concurrently schedules awaitables and completes after all of them finish, optionally returning exceptions as results.

asyncio.gather runs a sequence of awaitables concurrently and returns an awaitable that finishes only after every supplied operation completes. Awaiting it produces a list of results; coroutine arguments are automatically wrapped in tasks, so separate create_task calls are unnecessary. section 4.4

Use gather when a group of operations should run together and the next step needs the whole group.

  • Build a list of coroutines, expand it into gather, and await the returned result. section 4.4
  • Expect all operations to finish before processing the returned results; use as_completed instead when results should be handled as soon as they arrive. section 4.4 section 4.7
  • Leave return_exceptions false to propagate the first observed exception through await, or set it true to receive exceptions alongside successful results. section 4.4.1
  • Recognize that a failed awaitable does not automatically cancel the other running awaitables. section 4.4.1
  • Use gather beyond HTTP requests, including executor tasks, subprocesses, and custom task runners. section 7.2.3 section 13.1.2 section 14.1

Fetching many status codes concurrently

Create one coroutine per URL, then await them as a group: requests = [fetch_status(session, url) for url in urls]; status_codes = await asyncio.gather(*requests). The same structure can launch 1,000 requests concurrently and collect their status codes after all requests finish. For mixed success and failure, add return_exceptions=True and separate Exception instances from successful values. section 4.4 section 4.4.1

Listing 4.6 Running requests concurrently with gather
import asyncio
import aiohttp
from aiohttp import ClientSession
from chapter_04 import fetch_status
from util import async_timed
 
 
@async_timed()
async def main():
    async with aiohttp.ClientSession() as session:
        urls = ['https:/ / example .com' for _ in range(1000)]
        requests = [fetch_status(session, url) for url in urls]   #1
        status_codes = await asyncio.gather(*requests)            #2
        print(status_codes)
 
 
asyncio.run(main())

Shows the complete aiohttp pattern: create a client session, generate request coroutines, expand them into gather, and print the collected status codes. section 4.4

How Python Concurrency with asyncio covers it

How deep it goes
Explains awaitable wrapping, group completion, result collection, and exception modes, but defers lower-level task scheduling internals.
Examples
Uses concurrent HTTP status requests, delayed awaitables, invalid URLs, thread-pool work, encrypted subprocesses, and a custom task runner.
What it assumes
Assumes familiarity with coroutines, awaitables, async functions, and the asyncio event loop while demonstrating the grouping pattern.
What you can do after
Apply it via asyncio to launch awaitables concurrently, collect group results, and choose an exception-handling strategy.
How it connects
Connects coroutines, tasks, awaitables, aiohttp, as_completed, wait, executors, subprocesses, and semaphores.

Also explained in

Compare how 3 books explain asyncio.gather →

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.