Gunicorn Integration
Relevant source files
The following files were used as context for generating this wiki page:
Gunicorn integration allows Uvicorn to be used as a worker class within the Gunicorn process manager. This architecture combines Gunicorn's robust process management, signal handling, and worker oversight with Uvicorn's high-performance ASGI server capabilities. By using the UvicornWorker class, users can deploy ASGI applications behind a mature pre-fork worker model.
Sources: uvicorn/workers.py:21-25, README.md:104-106
Overview and Architecture
The integration is primarily implemented through the UvicornWorker class, which inherits from Gunicorn's base Worker. This class serves as a bridge that translates Gunicorn's configuration and process lifecycle events into Uvicorn's internal Config and Server instances.
Worker Hierarchy
The project provides two specialized worker classes:
UvicornWorker: A generic worker that defaults to automatic selection of event loops and HTTP protocols.UvicornH11Worker: A specialized version of the worker that explicitly uses theasyncioloop and theh11HTTP protocol implementation.
Sources: uvicorn/workers.py:21-24, uvicorn/workers.py:108-109
Component Relationship
The following diagram illustrates how Gunicorn interacts with Uvicorn components during the worker lifecycle:
The diagram shows the transition from Gunicorn's process management to Uvicorn's ASGI execution. Sources: uvicorn/workers.py:33-102, uvicorn/server.py
Worker Lifecycle and Signal Handling
The lifecycle of a Uvicorn worker within Gunicorn is managed through three primary methods: __init__, init_signals, and run.
Initialization
During __init__, Gunicorn's configuration (stored in self.cfg) is mapped to Uvicorn's Config arguments. This includes mapping timeouts, SSL settings, and limits.
| Gunicorn Setting | Uvicorn Mapping | Description |
|---|---|---|
self.cfg.keepalive | timeout_keep_alive | Keep-alive timeout for HTTP connections. |
self.timeout | timeout_notify | Timeout for the worker to notify the arbiter of its health. |
self.max_requests | limit_max_requests | Maximum requests before the worker is restarted. |
self.cfg.is_ssl | ssl_keyfile, etc. | Maps SSL options from Gunicorn to Uvicorn. |
Sources: uvicorn/workers.py:46-77
Execution and Signal Setup
The run() method is the entry point for the worker process. It utilizes asyncio_run to execute the private _serve() method, which prepares the Server and handles the socket provided by Gunicorn.
def run(self) -> None:
return asyncio_run(self._serve(), loop_factory=self.config.get_loop_factory())
async def _serve(self) -> None:
self.config.app = self.wsgi
server = Server(config=self.config)
self._install_sigquit_handler()
await server.serve(sockets=self.sockets)
Sources: uvicorn/workers.py:94-102
Signal Handling Integration
To prevent Gunicorn from interfering with Uvicorn's internal subprocess management (e.g., when Uvicorn might be running its own children), the worker resets standard signals to their default values. Specifically:
SIGUSR1: Handled viahandle_usr1without disturbing active requests.SIGQUIT: A custom handler is installed via_install_sigquit_handlerto ensure clean exit within the asyncio loop.
Sources: uvicorn/workers.py:79-92, CHANGELOG.md:315-316
Configuration and Defaults
The UvicornWorker defines a CONFIG_KWARGS dictionary to set specific protocol defaults for the Uvicorn instance.
class UvicornWorker(Worker):
CONFIG_KWARGS: dict[str, Any] = {"loop": "auto", "http": "auto"}
Sources: uvicorn/workers.py:27-28
Logging Configuration
The worker process redirects Uvicorn's internal loggers (uvicorn.error and uvicorn.access) to use Gunicorn's configured log handlers. This ensures that log output from Uvicorn is captured by Gunicorn's logging system, maintaining a single source of logs for the deployment.
Sources: uvicorn/workers.py:36-44
Deprecation Notice
As of recent updates, the uvicorn.workers module is considered deprecated. Users are encouraged to transition to the standalone uvicorn-worker package.
Sources: uvicorn/workers.py:14-18, CHANGELOG.md:246-247
Summary
The Gunicorn integration provides a bridge between a traditional pre-fork process model and the asynchronous ASGI world. By wrapping Uvicorn's Server and Config within a Gunicorn-compatible Worker class, the system leverages Gunicorn's mature process management (including health checks and scaling) while delivering the performance of Uvicorn's HTTP/1.1 and WebSocket implementations. This integration supports full SSL configuration and standard Unix signals, though it is currently transitioning toward a standalone package implementation.