# agronholm/anyio architecture

> Architecture diagram of the GitHub repository agronholm/anyio, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/agronholm/anyio
- Repository: https://github.com/agronholm/anyio

## Overview

AnyIO is a backend-neutral asynchronous networking and concurrency library. Applications call its APIs to run structured tasks and perform networking, stream I/O, synchronization, and blocking-work operations over either asyncio or Trio. Networking includes endpoint resolution, connection/listener creation, and optional TLS wrapping. The package also covers subprocesses, files, signals, cross-thread calls, subinterpreters, and async testing. The graph emphasizes the sampled event-loop, task, socket, stream, and backend paths; other README-listed capabilities are retained, but their internal call links are omitted where source was not sampled.

Main entry points: [`src/anyio/_core/_eventloop.py`](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_eventloop.py) (Event loop API), [`src/anyio/_core/_sockets.py`](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_sockets.py) (Socket operations), [`src/anyio/_core/_tasks.py`](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_tasks.py) (Task groups), [`src/anyio/_backends/_asyncio.py`](https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py) (Asyncio backend)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime and tasks"]
  node_eventloop["Event loop API<br/>[_eventloop.py]"]
  node_task_api["Task groups<br/>[_tasks.py]"]
  node_task_contracts["Task contracts<br/>[_tasks.py]"]
  node_async_backend["Asyncio backend<br/>[_asyncio.py]"]
  node_trio_backend["Trio backend<br/>[_trio.py]"]
  node_lowlevel["Low-level API<br/>[lowlevel.py]"]
end

subgraph group_network["Networking and streams"]
  node_socket_api["Socket operations<br/>[_sockets.py]"]
  node_socket_contracts["Socket contracts<br/>[_sockets.py]"]
  node_socket_backend["Backend sockets<br/>[_asyncio.py]"]
  node_stream_contracts["Stream contracts<br/>[_streams.py]"]
  node_memory_streams["Memory streams<br/>[memory.py]"]
  node_tls_streams["TLS streams<br/>[tls.py]"]
end

subgraph group_parallel["Blocking and parallel work"]
  node_thread_api["Worker threads<br/>[to_thread.py]"]
  node_process_api["Subprocesses<br/>[to_process.py]"]
  node_interpreter_api["Subinterpreters<br/>[to_interpreter.py]"]
  node_thread_portal["Thread portal<br/>[from_thread.py]"]
end

subgraph group_support["I/O and testing"]
  node_file_api["Async files<br/>[_fileio.py]"]
  node_sync_api["Synchronization"]
  node_functools_api["Async utilities<br/>[functools.py]"]
  node_itertools_api["Async iteration<br/>[itertools.py]"]
  node_signals["Signal handling<br/>[_signals.py]"]
  node_testing["Async testing<br/>[_testing.py]"]
  node_pytest["Pytest plugin<br/>[pytest_plugin.py]"]
end

node_app(("Application"))

node_app -->|"runs through"| node_eventloop
node_app -->|"starts tasks"| node_task_api
node_app -->|"uses networking"| node_socket_api
node_app -->|"offloads work"| node_thread_api
node_app -->|"runs subprocesses"| node_process_api
node_app -->|"parallelizes work"| node_interpreter_api
node_app -->|"uses async files"| node_file_api
node_app -->|"synchronizes tasks"| node_sync_api
node_app -->|"uses utilities"| node_functools_api
node_app -->|"uses iteration"| node_itertools_api
node_app -->|"handles signals"| node_signals
node_app -->|"tests asynchronously"| node_pytest
node_eventloop -.->|"selects backend"| node_async_backend
node_eventloop -.->|"selects backend"| node_trio_backend
node_task_api -->|"delegates to"| node_async_backend
node_task_api -->|"implements contracts"| node_task_contracts
node_async_backend -->|"implements"| node_task_contracts
node_trio_backend -->|"implements"| node_task_contracts
node_socket_api -->|"requests socket wrapping"| node_socket_backend
node_socket_api -->|"wraps with TLS"| node_tls_streams
node_socket_api -->|"uses contracts"| node_socket_contracts
node_async_backend -->|"implements"| node_socket_contracts
node_trio_backend -->|"implements"| node_socket_contracts
node_memory_streams -->|"implements"| node_stream_contracts
node_tls_streams -->|"implements"| node_stream_contracts
node_tls_streams -->|"offloads TLS work"| node_thread_api
node_file_api -->|"uses worker threads"| node_thread_api
node_thread_portal -->|"runs async tasks"| node_task_api
node_pytest -->|"uses event loop"| node_eventloop
node_lowlevel -.->|"exposes runtime controls"| node_eventloop

click node_eventloop "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_eventloop.py"
click node_task_api "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_tasks.py"
click node_task_contracts "https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_tasks.py"
click node_async_backend "https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py"
click node_trio_backend "https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_trio.py"
click node_lowlevel "https://github.com/agronholm/anyio/blob/master/src/anyio/lowlevel.py"
click node_socket_api "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_sockets.py"
click node_socket_contracts "https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_sockets.py"
click node_socket_backend "https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py"
click node_stream_contracts "https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_streams.py"
click node_memory_streams "https://github.com/agronholm/anyio/blob/master/src/anyio/streams/memory.py"
click node_tls_streams "https://github.com/agronholm/anyio/blob/master/src/anyio/streams/tls.py"
click node_thread_api "https://github.com/agronholm/anyio/blob/master/src/anyio/to_thread.py"
click node_process_api "https://github.com/agronholm/anyio/blob/master/src/anyio/to_process.py"
click node_interpreter_api "https://github.com/agronholm/anyio/blob/master/src/anyio/to_interpreter.py"
click node_thread_portal "https://github.com/agronholm/anyio/blob/master/src/anyio/from_thread.py"
click node_file_api "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_fileio.py"
click node_sync_api "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_synchronization.py"
click node_functools_api "https://github.com/agronholm/anyio/blob/master/src/anyio/functools.py"
click node_itertools_api "https://github.com/agronholm/anyio/blob/master/src/anyio/itertools.py"
click node_signals "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_signals.py"
click node_testing "https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_testing.py"
click node_pytest "https://github.com/agronholm/anyio/blob/master/src/anyio/pytest_plugin.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_eventloop,node_task_api,node_task_contracts,node_async_backend,node_trio_backend,node_lowlevel toneBlue
class node_socket_api,node_socket_contracts,node_socket_backend,node_stream_contracts,node_memory_streams,node_tls_streams toneAmber
class node_thread_api,node_process_api,node_interpreter_api,node_thread_portal toneMint
class node_file_api,node_sync_api,node_functools_api,node_itertools_api,node_signals,node_testing,node_pytest toneRose
class node_app toneIndigo
```

## Components

### Runtime and tasks

- [Event loop API](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_eventloop.py): `src/anyio/_core/_eventloop.py`
- [Task groups](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_tasks.py): `src/anyio/_core/_tasks.py`
- [Task contracts](https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_tasks.py): `src/anyio/abc/_tasks.py`
- [Asyncio backend](https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py): `src/anyio/_backends/_asyncio.py`
- [Trio backend](https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_trio.py): `src/anyio/_backends/_trio.py`
- [Low-level API](https://github.com/agronholm/anyio/blob/master/src/anyio/lowlevel.py): `src/anyio/lowlevel.py`

### Networking and streams

- [Socket operations](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_sockets.py): `src/anyio/_core/_sockets.py`
- [Socket contracts](https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_sockets.py): `src/anyio/abc/_sockets.py`
- [Backend sockets](https://github.com/agronholm/anyio/blob/master/src/anyio/_backends/_asyncio.py): `src/anyio/_backends/_asyncio.py`
- [Stream contracts](https://github.com/agronholm/anyio/blob/master/src/anyio/abc/_streams.py): `src/anyio/abc/_streams.py`
- [Memory streams](https://github.com/agronholm/anyio/blob/master/src/anyio/streams/memory.py): `src/anyio/streams/memory.py`
- [TLS streams](https://github.com/agronholm/anyio/blob/master/src/anyio/streams/tls.py): `src/anyio/streams/tls.py`

### Blocking and parallel work

- [Worker threads](https://github.com/agronholm/anyio/blob/master/src/anyio/to_thread.py): `src/anyio/to_thread.py`
- [Subprocesses](https://github.com/agronholm/anyio/blob/master/src/anyio/to_process.py): `src/anyio/to_process.py`
- [Subinterpreters](https://github.com/agronholm/anyio/blob/master/src/anyio/to_interpreter.py): `src/anyio/to_interpreter.py`
- [Thread portal](https://github.com/agronholm/anyio/blob/master/src/anyio/from_thread.py): `src/anyio/from_thread.py`

### I/O and testing

- [Async files](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_fileio.py): `src/anyio/_core/_fileio.py`
- [Synchronization](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_synchronization.py): `src/anyio/_core/_synchronization.py`
- [Async utilities](https://github.com/agronholm/anyio/blob/master/src/anyio/functools.py): `src/anyio/functools.py`
- [Async iteration](https://github.com/agronholm/anyio/blob/master/src/anyio/itertools.py): `src/anyio/itertools.py`
- [Signal handling](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_signals.py): `src/anyio/_core/_signals.py`
- [Async testing](https://github.com/agronholm/anyio/blob/master/src/anyio/_core/_testing.py): `src/anyio/_core/_testing.py`
- [Pytest plugin](https://github.com/agronholm/anyio/blob/master/src/anyio/pytest_plugin.py): `src/anyio/pytest_plugin.py`

### Other components

- **Application**

## Connections

- Application → Event loop API: runs through
- Application → Task groups: starts tasks
- Application → Socket operations: uses networking
- Application → Worker threads: offloads work
- Application → Subprocesses: runs subprocesses
- Application → Subinterpreters: parallelizes work
- Application → Async files: uses async files
- Application → Synchronization: synchronizes tasks
- Application → Async utilities: uses utilities
- Application → Async iteration: uses iteration
- Application → Signal handling: handles signals
- Application → Pytest plugin: tests asynchronously
- Event loop API → Asyncio backend: selects backend (evidence: `src/anyio/_backends/_asyncio.py`)
- Event loop API → Trio backend: selects backend (evidence: `src/anyio/_backends/_trio.py`)
- Task groups → Asyncio backend: delegates to (evidence: `src/anyio/_core/_tasks.py`)
- Task groups → Task contracts: implements contracts (evidence: `src/anyio/_core/_tasks.py`)
- Asyncio backend → Task contracts: implements (evidence: `src/anyio/_backends/_asyncio.py`)
- Trio backend → Task contracts: implements (evidence: `src/anyio/_backends/_trio.py`)
- Socket operations → Backend sockets: requests socket wrapping (evidence: `src/anyio/_core/_sockets.py`)
- Socket operations → TLS streams: wraps with TLS (evidence: `src/anyio/_core/_sockets.py`)
- Socket operations → Socket contracts: uses contracts (evidence: `src/anyio/abc/_sockets.py`)
- Asyncio backend → Socket contracts: implements (evidence: `src/anyio/_backends/_asyncio.py`)
- Trio backend → Socket contracts: implements (evidence: `src/anyio/_backends/_trio.py`)
- Memory streams → Stream contracts: implements (evidence: `src/anyio/streams/memory.py`)
- TLS streams → Stream contracts: implements (evidence: `src/anyio/streams/tls.py`)
- TLS streams → Worker threads: offloads TLS work (evidence: `src/anyio/streams/tls.py`)
- Async files → Worker threads: uses worker threads (evidence: `src/anyio/_core/_fileio.py`)
- Thread portal → Task groups: runs async tasks (evidence: `src/anyio/from_thread.py`)
- Pytest plugin → Event loop API: uses event loop (evidence: `src/anyio/pytest_plugin.py`)
- Low-level API → Event loop API: exposes runtime controls
