anyio
High-level concurrency and networking framework on top of asyncio or Trio
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.
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.
Install anyio
pip install anyio
import anyioUse 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.
Ready to explore the full API?
See every class, function, and relationship in an interactive graph.