|
| 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 | +``` |
0 commit comments