Web Frameworks & APIs

aiohttp

One toolkit that can both ask questions of the internet and answer them.

Install it: pip install aiohttp

What does it do?

Most tools pick a side — either fetching data from other websites or serving up your own — but aiohttp does both, without getting stuck waiting on any single one. Imagine an employee who can juggle answering incoming calls while also making outgoing ones, never letting one task block the other; that’s the async model aiohttp runs on. It can handle thousands of simultaneous web requests, whether it’s your program calling out to other services or other services calling into yours.

See it in action

This code has the computer visit a website on the internet, download its information, and print it out, without sitting idle while it waits.

import asyncio
import aiohttp

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://api.github.com") as response:
            print(await response.json())

asyncio.run(main())

Why would a non-developer care?

Any app that needs to fetch data from multiple places at once — pulling weather, stock prices, and news headlines simultaneously, say — runs faster and feels snappier when it isn’t forced to wait for each one in turn. That’s the practical, everyday payoff of the approach aiohttp champions.

Real-world examples

It’s part of the aio-libs collection of tools that many async Python projects rely on for both client requests and lightweight servers. Without an async approach like this, a program juggling many outside data sources would grind through them one at a time, and users would feel every second of that delay.

Who uses it

Developers building programs that need to fetch data from many sources simultaneously, or lightweight async servers that must juggle many connections at once.

How it compares to alternatives

On the client side, aiohttp is the async counterpart to the hugely popular Requests library, which works one call at a time. On the server side it overlaps with Tornado and Sanic, though aiohttp is often chosen specifically for its dual client-and-server flexibility.

Fun fact

aiohttp is maintained by the aio-libs organization, a collective of open-source contributors who built out an entire ecosystem of async-friendly tools once Python’s asyncio landed in the language in 2014.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries