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
