Skip to content

Commit 58f28be

Browse files
authored
Add section about event loop (#2725)
1 parent 93d9510 commit 58f28be

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

docs/concepts/event-loop.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Event Loop
2+
3+
Uvicorn provides two event loop implementations that you can choose from using the [`--loop`](../settings.md#implementation) option:
4+
5+
```bash
6+
uvicorn main:app --loop <auto|asyncio|uvloop>
7+
```
8+
9+
By default, Uvicorn uses `--loop auto`, which automatically selects:
10+
11+
1. **uvloop** - If [uvloop](https://github.com/MagicStack/uvloop) is installed, Uvicorn will use it for maximum performance
12+
2. **asyncio** - If uvloop is not available, Uvicorn falls back to Python's built-in asyncio event loop
13+
14+
Since `uvloop` is not compatible with Windows or PyPy, it is not available on these platforms.
15+
16+
On Windows, the asyncio implementation uses [`ProactorEventLoop`][asyncio.ProactorEventLoop] if running with multiple workers,
17+
otherwise it uses the standard [`SelectorEventLoop`][asyncio.SelectorEventLoop] for better performance.
18+
19+
??? info "Why does `SelectorEventLoop` not work with multiple processes on Windows?"
20+
If you want to know more about it, you can read the issue [#cpython/122240](https://github.com/python/cpython/issues/122240).
21+
22+
## Custom Event Loop
23+
24+
You can use custom event loop implementations by specifying a module path and function name using the colon notation:
25+
26+
```bash
27+
uvicorn main:app --loop <module>:<function>
28+
```
29+
30+
The function should return a callable that creates a new event loop instance.
31+
32+
### rloop
33+
34+
[rloop](https://github.com/gi0baro/rloop) is an experimental AsyncIO event loop implemented in Rust on top of the [mio](https://github.com/tokio-rs/mio) crate. It aims to provide high performance through Rust's systems programming capabilities.
35+
36+
You can install it with:
37+
38+
=== "pip"
39+
```bash
40+
pip install rloop
41+
```
42+
=== "uv"
43+
```bash
44+
uv add rloop
45+
```
46+
47+
You can run `uvicorn` with `rloop` with the following command:
48+
49+
```bash
50+
uvicorn main:app --loop rloop:new_event_loop
51+
```
52+
53+
!!! warning "Experimental"
54+
rloop is currently **experimental** and **not suited for production usage**. It is only available on **Unix systems**.
55+
56+
### Winloop
57+
58+
[Winloop](https://github.com/Vizonex/Winloop) is an alternative library that brings uvloop-like performance to Windows. Since uvloop is based on libuv and doesn't support Windows, Winloop provides a Windows-compatible implementation with significant performance improvements over the standard Windows event loop policies.
59+
60+
You can install it with:
61+
62+
=== "pip"
63+
```bash
64+
pip install winloop
65+
```
66+
=== "uv"
67+
```bash
68+
uv add winloop
69+
```
70+
71+
You can run `uvicorn` with `Winloop` with the following command:
72+
73+
```bash
74+
uvicorn main:app --loop winloop:new_event_loop
75+
```

mkdocs.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ nav:
5656
- ASGI: concepts/asgi.md
5757
- Lifespan: concepts/lifespan.md
5858
- WebSockets: concepts/websockets.md
59+
- Event Loop: concepts/event-loop.md
5960
- Deployment:
6061
- Deployment: deployment/index.md
6162
- Docker: deployment/docker.md
@@ -98,11 +99,8 @@ plugins:
9899
- mkdocstrings:
99100
handlers:
100101
python:
101-
import:
102-
- url: https://docs.python.org/3/objects.inv
103-
104-
plugins:
105-
- search
102+
inventories:
103+
- https://docs.python.org/3/objects.inv
106104
- llmstxt:
107105
full_output: llms-full.txt
108106
markdown_description: |-

0 commit comments

Comments
 (0)