Environment:Nautechsystems Nautilus trader Asyncio Uvloop Event Loop
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Live_Trading |
| Last Updated | 2026-02-10 08:30 GMT |
Overview
Asyncio event loop environment with uvloop 0.22.1 acceleration for high-performance live trading on Linux and macOS.
Description
NautilusTrader's live trading system is built on Python's `asyncio` framework. On Linux and macOS, it uses `uvloop` (a libuv-based event loop replacement) for significantly better I/O performance. The system kernel automatically sets the uvloop event loop policy at import time, unless running in a test environment (pytest). The `get_event_loop()` utility function provides safe event loop acquisition that is compatible with uvloop 0.22+ (which no longer auto-creates loops).
Usage
This environment is required for all live trading operations. The `TradingNode` and `LiveExecutionEngine` depend on an asyncio event loop for WebSocket connections, REST API calls, order management, and reconciliation. For backtesting, asyncio is not strictly required as the engine runs synchronously, but the kernel still initializes the event loop policy.
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Linux or macOS (for uvloop) | Windows uses standard asyncio loop |
| Python | >= 3.12 | asyncio improvements in 3.12+ are leveraged |
Dependencies
Python Packages
- `uvloop` == 0.22.1 (Linux/macOS only; pinned for stability)
Credentials
No credentials required.
Quick Install
# uvloop is automatically installed with nautilus_trader on Linux/macOS
pip install uvloop==0.22.1
# On Windows, uvloop is not available - asyncio stdlib loop is used
Code Evidence
Uvloop import guard from `system/kernel.py:90-98`:
try:
import uvloop
except ImportError: # pragma: no cover
uvloop = None
# Only set uvloop policy if not running in test environment,
# pytest-asyncio manages the event loop policy for tests via event_loop_policy fixture.
if uvloop is not None and "pytest" not in sys.modules:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
Safe event loop acquisition from `common/functions.py:24-98`:
def get_event_loop() -> asyncio.AbstractEventLoop:
try:
return asyncio.get_running_loop()
except RuntimeError:
pass # No running loop active
# Try the legacy semantics which respect preconfigured loops
loop = None
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = None
if loop is not None and not loop.is_closed():
return loop
# Fall back to the policy directly (uvloop 0.22+ requires this path)
# ...
# In test environments, do not create new loops to prevent resource leaks
if "pytest" in sys.modules:
msg = (
"No event loop available in test environment. "
"Use pytest-asyncio's 'event_loop' fixture parameter instead."
)
raise RuntimeError(msg)
# Production mode: create and register a new event loop
policy = policy or asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
policy.set_event_loop(loop)
return loop
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `RuntimeError: No event loop available in test environment` | Calling `get_event_loop()` inside pytest without fixture | Use `pytest-asyncio`'s `event_loop` fixture parameter |
| `ImportError: No module named 'uvloop'` | Running on Windows or uvloop not installed | Expected on Windows; install `uvloop` on Linux/macOS |
| `RuntimeError: There is no current event loop in thread` | Python 3.12+ removed auto-creation in non-main threads | Use `get_event_loop()` from `nautilus_trader.common.functions` |
Compatibility Notes
- Windows: `uvloop` is not available. NautilusTrader falls back to the standard `asyncio` event loop automatically. Performance may be lower for high-frequency live trading.
- pytest: The uvloop policy is intentionally NOT set during tests. `pytest-asyncio` manages the event loop policy via its `event_loop_policy` fixture.
- uvloop 0.22+: This version no longer auto-creates event loops in `asyncio.get_event_loop()`. The `get_event_loop()` utility handles this by falling back to the policy directly.