Deployment & ASGI Servers
Relevant source files
The following files were used as context for generating this wiki page:
Starlette is a lightweight ASGI (Asynchronous Server Gateway Interface) framework and toolkit designed for building high-performance asyncio services. Deployment of a Starlette application requires an ASGI server to bridge the gap between the network and the application code. Because Starlette is built on the ASGI standard, it is compatible with various production-grade servers and deployment environments, ranging from traditional virtual machines to serverless platforms.
The framework supports multiple concurrency backends, including asyncio and trio, and is designed to be used either as a complete framework or as a modular toolkit where individual components (like responses or routing) can be used independently within any ASGI application.
Sources: README.md:1-25, README.md:72-80
ASGI Server Compatibility
To run a Starlette application in production, an ASGI server is required. These servers handle the low-level socket communication, HTTP parsing, and task scheduling, passing normalized scope, receive, and send primitives to the Starlette application.
Recommended Servers
The following ASGI servers are commonly used to host Starlette applications:
| Server | Description |
|---|---|
| Uvicorn | A lightning-fast ASGI server implementation, using uvloop and httptools. |
| Daphne | The original ASGI server, used primarily with Django Channels. |
| Hypercorn | An ASGI server based on the hyper libraries and supports HTTP/2. |
Sources: README.md:31-35
Execution Flow
When a request arrives, the ASGI server creates a connection scope and invokes the Starlette application. The application then processes the request through its middleware stack and router.
The diagram shows the lifecycle of a request from the client through the ASGI server into the Starlette framework stack.
Sources: README.md:82-90, starlette/applications.py:79-83
Application Configuration for Deployment
The Starlette Instance
The core of deployment is the Starlette class, which serves as the primary ASGI entry point. It manages the middleware stack, exception handlers, and routing table.
from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import JSONResponse
async def homepage(request):
return JSONResponse({'hello': 'world'})
app = Starlette(debug=False, routes=[Route("/", homepage)])
Sources: README.md:41-54, starlette/applications.py:20-53
Middleware Stack Construction
Starlette automatically wraps the application in a specific middleware order during initialization to ensure production stability. ServerErrorMiddleware is placed at the outermost layer to catch any unhandled exceptions before they reach the ASGI server.
| Order | Middleware | Purpose |
|---|---|---|
| 1 (Outermost) | ServerErrorMiddleware | Handles 500 errors and provides debug tracebacks. |
| 2 (User) | User Middleware | Custom middleware added via add_middleware(). |
| 3 (Innermost) | ExceptionMiddleware | Handles specific exceptions (e.g., 404, 405) from the router. |
Sources: starlette/applications.py:59-71, starlette/middleware/errors.py:126-135
Deployment Environments
Serverless and Edge Deployment
Starlette applications can be deployed to edge platforms using specialized configuration files. For example, Cloudflare Workers deployment utilizes a wrangler.toml file to define assets and compatibility dates.
name = "starlette"
compatibility_date = "2026-05-22"
workers_dev = true
[assets]
directory = "./site"
not_found_handling = "404-page"
Sources: wrangler.toml:1-8
Legacy WSGI Integration
For deployment in environments that only support WSGI (Web Server Gateway Interface), Starlette provides a WSGIMiddleware. This allows Starlette to wrap legacy synchronous applications, though it is currently marked as deprecated in favor of external libraries like a2wsgi.
The sequence shows how WSGIMiddleware translates asynchronous ASGI messages into a synchronous WSGI environment.
Sources: starlette/middleware/wsgi.py:12-25, starlette/middleware/wsgi.py:100-120
Lifespan Management
In production, applications often need to manage resources like database connection pools. Starlette uses a lifespan handler to manage startup and shutdown events communicated by the ASGI server.
- Startup: Triggered when the ASGI server is ready to serve requests.
- Shutdown: Triggered when the ASGI server is performing a graceful exit.
Sources: starlette/applications.py:48-52, tests/test_routing.py:593-614
Summary
Deployment of Starlette focuses on its implementation of the ASGI specification, enabling high-performance asynchronous execution. By utilizing standard ASGI servers like Uvicorn and maintaining a robust middleware stack (particularly for error handling), Starlette provides a production-ready environment for modern Python web services.