AnyIO
AnyIO is an asynchronous networking and concurrency library that works on top of either asyncio or Trio. It implements Trio-like structured concurrency (SC) on top of asyncio and works in harmony with the native SC of Trio itself.
Applications and libraries written against AnyIO’s API will run unmodified on either asyncio or Trio. AnyIO can also be adopted into a library or application incrementally – bit by bit, no full refactoring necessary. It will blend in with the native libraries of your chosen backend.
To find out why you might want to use AnyIO’s APIs instead of asyncio’s, you can read about it here.
Documentation
View full documentation at: https://anyio.readthedocs.io/
Features
AnyIO offers the following functionality:
Task groups (nurseries in trio terminology)
High-level networking (TCP, UDP and UNIX sockets)
Happy eyeballs algorithm for TCP connections (more robust than that of asyncio on Python 3.8)
async/await style UDP sockets (unlike asyncio where you still have to use Transports and Protocols)
A versatile API for byte streams and object streams
Inter-task synchronization and communication (locks, conditions, events, semaphores, object streams)
Worker threads
Subprocesses
Subinterpreter support for code parallelization (on Python 3.13 and later)
Asynchronous file I/O (using worker threads)
Signal handling
Asynchronous version of the functools module
AnyIO also comes with its own pytest plugin which also supports asynchronous fixtures. It even works with the popular Hypothesis library.
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
The manual
- The basics
- Creating and managing tasks
- Cancellation and timeouts
- Using synchronization primitives
- Streams
- Using typed attributes
- Using sockets and streams
- Working with threads
- Running a function in a worker thread
- Calling asynchronous code from a worker thread
- Calling synchronous code from a worker thread
- Accessing the event loop from a foreign thread
- Running code from threads using blocking portals
- Context propagation
- Adjusting the default maximum worker thread count
- Reacting to cancellation in worker threads
- Using subprocesses
- Working with subinterpreters
- Asynchronous file I/O support
- Asynchronous Temporary File and Directory
- Receiving operating system signals
- Context manager mix-in classes
- Testing with AnyIO
- API reference
- Event loop
- Asynchronous resources
- Typed attributes
- Timeouts and cancellation
- Task groups
- Running code in worker threads
- Running code in subinterpreters
- Running code in worker processes
- Running asynchronous code from other threads
- Async file I/O
- Temporary files and directories
- Context manager mix-in classes
- Streams and stream wrappers
- Sockets and networking
- Subprocesses
- Synchronization
- Operating system signals
- Asynchronous functools
- Low level operations
- Testing and debugging
- Exceptions
- Migrating from AnyIO 3 to AnyIO 4
- Migrating from AnyIO 2 to AnyIO 3
- Why you should be using AnyIO APIs instead of asyncio APIs
- Design problems with task management
- Design problems with cancellation
- Design problems with asyncio queues
- Design problems with the streams API
- Design problems with the thread API
- Design problems with signal handling APIs
- Missing file I/O and async path support
- Features not in asyncio which you might be interested in
- Frequently Asked Questions
- Getting help
- Reporting bugs
- Contributing to AnyIO
- Version history