Introduction
Granian is an HTTP server for Python web applications, implemented in Rust using the hyper HTTP library. It serves both ASGI and WSGI applications, making it a drop-in replacement for Uvicorn, Gunicorn, or Hypercorn. By handling connection management, HTTP parsing, and the event loop in compiled Rust code, Granian reduces per-request overhead.
What Granian Does
- Serves ASGI applications (FastAPI, Starlette, Django Channels) with async support
- Serves WSGI applications (Flask, Django) with synchronous handling
- Manages multiple worker processes and threads for parallel request handling
- Supports HTTP/1.1 and HTTP/2 out of the box
- Provides WebSocket support for ASGI applications
Architecture Overview
Granian embeds a Rust HTTP server built on hyper and Tokio. Each worker process runs a Tokio async runtime that accepts connections and parses HTTP requests in Rust. Requests are passed to Python through PyO3 bindings, invoking the ASGI or WSGI callable. Responses flow back through Rust for serialization and transmission. This architecture keeps the network I/O layer in compiled code while the application logic remains in Python.
Self-Hosting & Configuration
- Install with
pip install granian(requires Python 3.8+ and a Rust toolchain at build time, or use prebuilt wheels) - Specify the interface with
--interface asgior--interface wsgi - Configure workers with
--workers Nand threads per worker with--threads N - Bind to a specific address and port with
--hostand--port - Enable access logging, TLS, and Unix socket binding through CLI flags
Key Features
- Rust-based I/O layer using hyper and Tokio for high throughput and low latency
- Supports both ASGI and WSGI interfaces in a single server
- HTTP/2 support with no additional configuration
- Multi-process and multi-thread scaling via simple CLI options
- Prebuilt wheels for Linux, macOS, and Windows on PyPI
Comparison with Similar Tools
- Uvicorn — Popular ASGI server using uvloop; Granian replaces the Python event loop with a Rust runtime for lower overhead
- Gunicorn — Battle-tested WSGI server; Granian serves both WSGI and ASGI and handles HTTP parsing in Rust
- Hypercorn — ASGI server with HTTP/2 and HTTP/3; Granian focuses on performance through its Rust implementation
- Daphne — Django Channels' ASGI server; Granian is framework-agnostic and offers higher throughput
FAQ
Q: Can I use Granian with FastAPI?
A: Yes. Run granian --interface asgi myapp:app where app is your FastAPI instance.
Q: Does it support Django?
A: Yes. For standard Django, use --interface wsgi. For Django Channels, use --interface asgi.
Q: How does performance compare to Uvicorn? A: Benchmarks show Granian handling more requests per second with lower tail latency on typical ASGI workloads, though results vary by application.
Q: Do I need Rust installed? A: No. Prebuilt binary wheels are available on PyPI for common platforms. Rust is only needed if building from source.