Granian: How a Rust‑Based ASGI Server Makes Python Web Apps Lightning‑Fast
This article explains the WSGI/ASGI ecosystem, compares uWSGI, Nginx, and uvicorn, and then introduces Granian—a Rust‑written ASGI server that supports WSGI, ASGI, and RSGI, offering superior performance, HTTP/2‑3 support, and easy integration for Python web applications.
Before discussing Granian, the article reviews the core concepts of Python web deployment: WSGI (Web Server Gateway Interface) defines the communication protocol between a web server and a Python web application, while uWSGI is a WSGI‑compatible server that also implements the uwsgi and HTTP protocols. Nginx, although also a web server, adds reverse‑proxy, load‑balancing, static‑file caching, and more, so it is typically placed in front of uWSGI, which then exposes a TCP service for Nginx to forward HTTP requests.
WSGI only supports a request/response lifecycle, which prevents it from handling long‑lived connections such as WebSockets or asynchronous workloads. To address this, ASGI (Asynchronous Server Gateway Interface) was created, extending WSGI with coroutine‑based APIs that enable non‑blocking I/O, long polling, WebSockets, and HTTP/2.
# main.py (WSGI example)
def application(env, start_response):
start_response("200 OK", [("Content-Type", "text/html")])
return [b"WSGI hello!"] # main.py (ASGI example)
async def application(scope, receive, send):
await send({"type": "http.response.start", "status": 200, "headers": [(b"Content-Type", b"text/html")]})
await send({"type": "http.response.body", "body": b"ASGI hello!"})The most popular ASGI server is uvicorn, built on uvloop and httptools. It can be installed with pip install uvicorn and started via uvicorn main:application. On Windows, uvloop falls back to the standard asyncio loop.
Granian is introduced as an alternative ASGI server written in Rust. Its advantages stem from Rust’s memory safety and performance guarantees. Granian fully supports the Python asynchronous model, can run any ASGI‑compatible framework (e.g., FastAPI, BlackSheep), and adds support for the latest web protocols such as HTTP/2 and HTTP/3. It also leverages multiple CPU cores for higher concurrency and offers a simple integration path.
Installation is straightforward: pip install granian. A minimal BlackSheep application is shown, and the service is launched with granian main:app --interface asgi. Tests reported that the Granian + BlackSheep combination outperforms comparable setups.
Granian supports three interfaces:
WSGI – the classic Web Server Gateway Interface.
ASGI – the asynchronous counterpart.
RSGI – a Rust‑specific Server Gateway Interface.
Example code for each interface is provided, followed by the corresponding launch commands:
# WSGI
def wsgi_application(env, start_response):
start_response("200 OK", [("Content-Type", "text/html")])
return [b"WSGI hello!"]
# ASGI
async def asgi_application(scope, receive, send):
await send({"type": "http.response.start", "status": 200, "headers": [(b"Content-Type", b"text/html")]})
await send({"type": "http.response.body", "body": b"ASGI hello!"})
# RSGI
async def rsgi_application(scope, proto):
assert scope.proto == "http"
proto.response_str(status=200, headers=[("content-type", "text/html")], body="RSGI Hello!")Launch commands:
granian main:wsgi_application --interface wsgi granian main:asgi_application --interface asgi granian main:rsgi_application --interface rsgiBecause the RSGI interface lacks framework support at present, the article recommends using ASGI for most cases. Additional Granian CLI options such as --host, --port, and --workers are mentioned, with detailed help available via granian --help.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Satori Komeiji's Programming Classroom
Python and Rust developer; I write about any topics you're interested in. Follow me! (#^.^#)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
