Pythonanyiov4.13.0
PythonBeginner-friendly

anyio

High-level concurrency and networking framework on top of asyncio or Trio

asyncasynciotrioconcurrencycompatibility
The Problem

Python has two competing async runtimes: asyncio (built-in) and trio (third-party). Libraries that hardcode asyncio can't be used in trio applications and vice versa. Framework authors had to pick one and exclude the other. anyio provides a compatibility layer so library authors can write async code once and have it work on both backends.

Mental Model

anyio is an abstraction over async primitives โ€” task groups, cancellation, sockets, file I/O, locks. When you import from anyio, you get APIs that run correctly whether the underlying runtime is asyncio or trio. Applications choose the backend at startup; libraries built on anyio just work on either.

60-second start

Install anyio

pip install anyio
import anyio

Use it when

Use anyio when writing an async library or framework that you want to be runtime-agnostic. It's also useful in applications that want trio's structured concurrency model but need to interop with asyncio-based libraries like FastAPI or aiohttp.

Skip it when

For application code that commits to asyncio (e.g. a FastAPI app), using asyncio directly is simpler โ€” there's no need for the compatibility layer. anyio shines at the library level, not the application level.

Alternatives
asyncioPython's built-in async runtime โ€” sufficient for applications that don't need trio compatibility
trioAlternative async runtime with a cleaner structured concurrency model; use anyio on top of it to maintain compatibility
curioExperimental async library from the asyncio author; mostly superseded by trio and asyncio improvements

Ready to explore the full API?

See every class, function, and relationship in an interactive graph.