A Zig implementation of a lightning-fast ASGI server, inspired by uvicorn.
This project aims to rewrite the popular Python ASGI server uvicorn in Zig, taking advantage of Zig's performance characteristics and safety guarantees. The development approach is exploratory and iterative, focusing on understanding and reimplementing uvicorn's core functionality while maintaining its speed and reliability.
This is a work in progress. The project is being developed through "vibe coding" - an intuitive, exploratory approach to understanding and reimplementing uvicorn's architecture and features.
- Provide a high-performance ASGI server implementation in Zig
- Maintain compatibility with ASGI specification
- Learn and document the process of porting Python async code to Zig
- Explore Zig's concurrency and networking capabilities
- Basic HTTP server implementation
- More features coming soon!
The project includes comprehensive unit tests and integration tests to ensure reliability and correctness.
To run all unit tests (recommended):
zig build testFor more detailed test output:
zig build test --summary allzig build test- Runs all unit tests (library and executable tests)zig build test-python- Runs Python integration testszig build --help- Shows all available build steps and options
You can test specific components by running individual test files:
# Test ASGI protocol functionality
zig test src/asgi/protocol_test.zig
# Test HTTP server functionality
zig test src/http/server_test.zig
# Test WebSocket server functionality
zig test src/websocket/server_test.zig
# Test utility functions
zig test src/utils/common_test.zig
# Test Python integration
zig test src/python/integration_test.zig- Unit Tests: Built into library and executable modules, covering core functionality
- Integration Tests: Separate test files for specific components (HTTP, WebSocket, ASGI)
- Python Integration Tests: Tests for Python-Zig interoperability and ASGI communication
For continuous testing during development, you can use:
zig build test --watchThis will automatically rerun tests when source files change.
main(): Entry point that sets up the server based on CLI argumentsrunMaster(): Manages worker processes when running in multi-worker moderunWorker(): Handles the actual server work in a single processhandleConnection(): Processes incoming HTTP requests
handleLifespan(): Manages application startup/shutdown eventscallAsgiApplication(): Calls the Python ASGI application with required parameterscreateHttpScope(): Creates the HTTP scope dictionary for ASGIcreateWebSocketScope(): Creates WebSocket scope for ASGIMessageQueue: Handles message passing between server and application
loadApplication(): Loads the Python ASGI application from a moduletoPyString()/fromPyString(): Convert between Zig and Python stringsjsonToPyObject()/pyObjectToJson(): Convert between JSON and Python objectscreateReceiveCallable()/createSendCallable(): Create Python functions for ASGI interface
handleWebSocketConnection(): Manages WebSocket connectionshandshake(): Performs the WebSocket protocol handshakeConnection.send()/Connection.receive(): Send/receive WebSocket messages
Logger: Provides logging functionality with different severity levelsWorkerPool: Manages worker processes for multi-process modeOptions: Handles command-line arguments and configuration
The architecture follows the ASGI specification, using Python integration to communicate with Python web frameworks while providing HTTP and WebSocket handling in Zig.
For a basic FastAPI app (/hello returning JSON), here's a rough comparison:
| Server | Avg Latency (ms) | Requests/sec |
|---|---|---|
| Uvicorn (default) | ~1.5 - 3.0 ms | ~50,000 - 80,000 |
| Zig-based ASGI | ~0.5 - 2.0 ms | ~70,000 - 120,000 |
- You could see a 20-50% increase in requests per second.
- Latency could drop by 30-70%, especially under high concurrency.
- CPU & memory usage would likely be lower compared to Uvicorn.
This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.
This project is inspired by and based on concepts from the Uvicorn project, which is also licensed under the BSD 3-Clause License.