# uvloop — Ultra-Fast asyncio Event Loop for Python > A drop-in replacement for the default asyncio event loop, built on libuv, that makes Python async networking significantly faster. ## Install Save as a script file and run: # uvloop — Ultra-Fast asyncio Event Loop for Python ## Quick Use ```bash pip install uvloop ``` ```python import asyncio import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) async def main(): # your async code runs on uvloop now await asyncio.sleep(1) asyncio.run(main()) ``` ## Introduction uvloop is a fast, drop-in replacement for the built-in asyncio event loop in Python. It is implemented in Cython and wraps libuv, the same high-performance I/O library that powers Node.js. Switching to uvloop typically requires only two lines of code and can double or triple the throughput of asyncio-based applications. ## What uvloop Does - Replaces the default asyncio event loop with a libuv-backed implementation - Accelerates TCP, UDP, and Unix socket operations for async Python code - Provides full compatibility with the standard asyncio API - Improves performance of frameworks like aiohttp, Sanic, and FastAPI - Supports DNS resolution, subprocess management, and signal handling ## Architecture Overview uvloop is written in Cython and compiles to a C extension module. It wraps libuv handles and requests to implement the asyncio event loop interface. Socket I/O, timers, signal watchers, and child process management all delegate to libuv's non-blocking, cross-platform primitives. Because it follows the asyncio abstract base class contract, existing coroutines and libraries work without modification. ## Self-Hosting & Configuration - Install with `pip install uvloop` (requires Python 3.8+) - Activate globally with `asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())` - Or use `uvloop.install()` as a shortcut in Python 3.12+ - No configuration files are needed; it is a runtime swap - Works on Linux and macOS (Windows is not supported by libuv's full feature set in this context) ## Key Features - Two to four times faster than the default asyncio loop on network benchmarks - Zero API changes required for existing asyncio code - Built on libuv, a proven C library with years of production use in Node.js - Handles millions of connections efficiently with low memory overhead - Used by Uvicorn, the default ASGI server for FastAPI and Starlette ## Comparison with Similar Tools - **asyncio (default loop)** — Standard library implementation; uvloop is a faster drop-in alternative - **Trio** — Alternative async library with structured concurrency; uvloop stays within the asyncio ecosystem - **curio** — Minimal async kernel; uvloop targets the standard asyncio interface for broader compatibility - **libuv (raw)** — C library for async I/O; uvloop wraps it and exposes it through Python's asyncio API ## FAQ **Q: Does uvloop work on Windows?** A: No. libuv on Windows uses IOCP which has different semantics. uvloop officially supports Linux and macOS. **Q: Will my existing asyncio code break?** A: No. uvloop implements the full asyncio event loop interface. Existing coroutines, tasks, and libraries work without changes. **Q: How do I use uvloop with Uvicorn?** A: Uvicorn uses uvloop by default when it is installed. Just `pip install uvloop` and Uvicorn will auto-detect it. **Q: Is there a performance cost to using Cython?** A: The Cython compilation step happens at package build time. At runtime, uvloop executes as native C code with no interpretation overhead. ## Sources - https://github.com/MagicStack/uvloop - https://uvloop.readthedocs.io --- Source: https://tokrepo.com/en/workflows/asset-4519c1b4 Author: Script Depot