liveBook · Manning

Aiohttp

As explained in Python Concurrency with asyncio

aiohttp is a Python asynchronous HTTP client and server library built on asyncio.

aiohttp is an asynchronous HTTP client and server library for Python and asyncio. It uses non-blocking sockets and returns coroutines, allowing web requests to run without halting the event loop. section 4.1 section 4.7

Use the client for concurrent requests to REST APIs, web crawling, and other workloads involving many network operations. Acquire and release HTTP sessions with async with so resources are cleaned up even after exceptions. section 4.0 section 4.1

aiohttp works with asyncio coordination patterns:

  • Use gather to start multiple coroutines concurrently and wait for all results. section 4.7
  • Use as_completed to process responses as soon as individual requests finish. section 4.7
  • Use wait to distinguish completed tasks from those still running, then cancel or await selected tasks. section 4.7
  • Set individual or group timeouts and cancel in-flight requests when results or failures require it. section 4.0

Why a non-blocking client matters

Creating several asyncio tasks around requests.get does not make requests concurrent: the blocking socket pauses the single event loop, so requests run one after another. Replacing the blocking client with aiohttp preserves coroutine-based concurrency. section 2.7.2

Beyond client requests, aiohttp can create asyncio-ready web application servers and can support microservice APIs that use concurrency for performance. section 9.1 section 10.0

How Python Concurrency with asyncio covers it

How deep it goes
Explains non-blocking sockets, sessions, context managers, concurrency coordination, timeouts, cancellation, and server use without lower-level implementation details.
Examples
Uses hundreds of URLs, concurrent REST requests, HTTP sessions, asyncio.gather, as_completed, wait, timeouts, cancellation, and REST API servers.
What it assumes
Assumes basic asyncio, coroutines, await, tasks, sockets, and event-loop behavior while introducing asynchronous context managers from their resource-management role.
What you can do after
Build it with asyncio to issue concurrent HTTP requests, process results by completion, manage sessions, cancel tasks, and create web servers.
How it connects
Connects asynchronous context managers, asyncio, coroutines, non-blocking sockets, REST APIs, web crawling, microservices, and event loops.

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.