The basics

AnyIO requires Python 3.10 or later to run. It is recommended that you set up a virtualenv when developing or playing around with AnyIO.

Installation

To install AnyIO, run:

pip install anyio

To install a supported version of Trio, you can install it as an extra like this:

pip install anyio[trio]

Running async programs

The simplest possible AnyIO program looks like this:

from anyio import run


async def main() -> None:
    print("Hello, world!")


run(main)

This will run the program above on the default backend (asyncio). To explicitly specify which backend to run on, you can use the backend argument, like so:

run(main, backend='trio')
run(main, backend='asyncio')

But AnyIO code is not required to be run via run(). You can just as well use the native run() function of the backend library:

import sniffio
import trio

from anyio import sleep


async def main() -> None:
    print("Hello")
    await sleep(1)
    print("I'm running on", sniffio.current_async_library())


trio.run(main)

Changed in version 4.0.0: On the asyncio backend, anyio.run() now uses a back-ported version of asyncio.Runner on Pythons older than 3.11.

Backend specific options

Any options exclusive to a specific backend can be passed with the backend_options argument to run():

run(main, backend="asyncio", backend_options={"debug": True})
run(
    main,
    backend="trio",
    backend_options={"restrict_keyboard_interrupt_to_checkpoints": True}
)

Here is the list of supported options for each backend:

Asyncio:

  • options covered in the documentation of asyncio.Runner

  • use_uvloop (bool, default=False): Use the faster uvloop event loop implementation (this is a shorthand for passing loop_factory=uvloop.new_event_loop, or loop_factory=winloop.new_event_loop if using Windows, and is ignored if loop_factory is passed a value other than None)

Setting use_uvloop=True requires uvloop to be installed, or winloop on Windows. These are optional dependencies and are not installed with AnyIO. Install the appropriate package with pip install uvloop or pip install winloop. If it is missing, starting the event loop raises ModuleNotFoundError rather than falling back to the standard asyncio event loop. Leave use_uvloop at its default of False to use the standard event loop.

Trio: options covered in the official documentation

Changed in version 3.2.0: The default value of use_uvloop was changed to False.

Changed in version 4.0.0: The policy option was replaced with loop_factory.

Using native async libraries

AnyIO lets you mix and match code written for AnyIO and code written for the asynchronous framework of your choice. There are a few rules to keep in mind however:

  • You can only use “native” libraries for the backend you’re running, so you cannot, for example, use a library written for Trio together with a library written for asyncio.

  • Tasks spawned by these “native” libraries on backends other than Trio are not subject to the cancellation rules enforced by AnyIO

  • Threads spawned outside of AnyIO cannot use from_thread.run() to call asynchronous code