Task Queues & Scheduling

anyio

One set of instructions that works whether your program is built on asyncio or on Trio, no translation needed.

Install it: pip install anyio

What does it do?

Python actually has two different async concurrency systems in common use, the built-in asyncio and the independently built Trio, and code written for one traditionally can’t run on the other. AnyIO is a compatibility layer that lets library authors write their concurrency code once and have it run on top of either system, like an appliance that works with both US and European power outlets. Application developers benefit too: they can pick asyncio or Trio for their own reasons and still use libraries built on AnyIO without conflicts.

See it in action

This runs two greeting tasks side by side, each with its own short pause before printing, using code that works the same way no matter which underlying async system Python is using.

import anyio

async def say(text, delay):
    await anyio.sleep(delay)
    print(text)

async def main():
    async with anyio.create_task_group() as tg:
        tg.start_soon(say, "hello", 1)
        tg.start_soon(say, "world", 2)

anyio.run(main)

Why would a non-developer care?

Without something like AnyIO, the Python async ecosystem would be more fractured than it already occasionally feels, with library authors forced to pick a side or maintain two versions of everything. It quietly reduces the amount of sorry-this-library-doesn’t-support-your-async-framework friction developers run into.

Real-world examples

AnyIO is used under the hood by major web projects, including parts of the Starlette and FastAPI ecosystem, to support both asyncio and Trio users without duplicating code. Without it, a library maintainer wanting to support both async ecosystems would have to write and test everything twice, a significant, ongoing maintenance burden most projects can’t afford.

Who uses it

Library authors and framework developers who want their code to work for both asyncio and Trio users without writing everything twice.

How it compares to alternatives

It doesn’t compete with asyncio or Trio so much as sit on top of both simultaneously; its real point of comparison is simply not using an abstraction layer and picking one ecosystem to support exclusively.

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

Find a beginner-friendly course

Related libraries