Gunicorn Documentation

repository·master·Indexed 27 days ago

https://github.com/benoitc/gunicorn

Gunicorn ('Green Unicorn') is a Python WSGI HTTP Server for UNIX using a pre-fork worker model. It is compatible with Python 3.9+ and supports a wide range of web frameworks, including WSGI (Django, Flask, Pyramid) and ASGI (FastAPI, Starlette, Quart). It features multiple worker types (sync, gthread, gevent, asgi), Nginx integration via the uWSGI binary protocol, and advanced lifecycle management through server hooks.

Tokens
47.6K
Snippets
124
Records
326
Agent score
94%

What's inside Gunicorn

  1. Understand Gunicorn configuration priority

    master

    Gunicorn resolves configuration settings from five sources. If multiple sources define the same setting, the source with the higher priority wins. The priority order (from lowest to highest) is:

    1. Environment variables (only for settings that support them).
    2. Framework-specific configuration (currently only supported for Paste Deploy).
    3. Python configuration file (defaults to gunicorn.conf.py in the working directory).
    4. GUNICORN_CMD_ARGS environment variable.
    5. Command-line arguments.

    Note: If a configuration file is specified via both GUNICORN_CMD_ARGS and the CLI, only the file specified on the command line is used.

  2. Supported frameworks and worker types

    master

    Gunicorn is compatible with various WSGI and ASGI frameworks and offers different worker models depending on your workload:

    Supported Frameworks

    • WSGI: Django, Flask, Pyramid, Falcon, Bottle
    • ASGI: FastAPI, Starlette, Quart

    Worker Types

    • Sync: The default model. One request per worker. Simple and predictable.
    • Async (Gevent/Eventlet): Designed for I/O-bound workloads, handling thousands of concurrent connections.
    • Threads: Multiple threads per worker to balance concurrency and simplicity.
    • ASGI: Native asyncio support for async frameworks like FastAPI and Starlette.
  3. Gunicorn Features Overview

    master

    Gunicorn ('Green Unicorn') is a Python WSGI HTTP Server for UNIX using a pre-fork worker model. Key features include:

    • WSGI Support: Compatible with Django, Flask, Pyramid, and other WSGI frameworks.
    • ASGI Support: Compatible with FastAPI, Starlette, and Quart.
    • HTTP/2 Support (beta): Includes multiplexed streams.
    • Dirty Arbiters (beta): Designed for heavy workloads like ML models or long-running tasks.
    • Worker Types: Supports sync, gthread, gevent, and asgi worker classes.
    • Nginx Integration: Supports the uWSGI binary protocol.
    • Compatibility: Works with Python 3.9+.
  4. Upgrade Gunicorn binary without downtime

    master

    You can replace the Gunicorn binary without downtime using a binary upgrade procedure. Incoming requests continue to be served during the transition.

    Upgrade Steps

    1. Replace the old binary.
    2. Send USR2 to the old master. A new master starts (its PID file will end in .2) and spawns new workers.
    3. Send WINCH to the old master to gracefully stop its workers.
    4. Once the new master is running successfully, send TERM to the old master to complete the upgrade.

    Rollback Procedure

    If the new version is problematic, you can roll back while the old master still holds the listen sockets:

    1. Send HUP to the old master to restart its workers without reloading the config file.
    2. Send TERM to the new master to shut down its workers gracefully.
    3. Send QUIT to the new master to force it to exit.
    4. If new workers linger, send KILL after the new master quits.

    Note: Since version 19.6.0, PID files follow the pattern <name>.pid.2 instead of <name>.pid.oldbin.

  5. Deploy ASGI with Nginx (uWSGI Protocol)

    master

    For improved performance, you can use the uWSGI protocol with Nginx's uwsgi_pass.

    Warning: WebSocket connections are not supported when using the uWSGI protocol. Use an HTTP proxy for WebSocket endpoints.

    gunicorn myapp:app --worker-class asgi --protocol uwsgi --bind 127.0.0.1:8000
    upstream gunicorn {
        server 127.0.0.1:8000;
    }
    
    server {
        listen 80;
        server_name example.com;
    
        location / {
            uwsgi_pass gunicorn;
            include uwsgi_params;
        }
    }
  6. Use the gunicornc CLI to manage Gunicorn

    master

    The gunicornc CLI is used to interact with the Gunicorn control socket. You can use it in Interactive Mode (by running gunicornc without arguments) or via Single Commands for scripting.

    Common CLI Flags:

    • -s <path>: Connect to a custom socket path.
    • -c <command>: Run a single command and exit.
    • -j: Output the result as JSON (ideal for scripting).

    Example usage:

    # Connect to default socket
    gunicornc
    
    # Connect to custom socket
    gunicornc -s /tmp/myapp.ctl
    
    # Run a single command
    gunicornc -c "show workers"
    
    # Output as JSON for scripting
    gunicornc -c "show stats" -j
    gunicornc -c "show stats" -j
  7. Configure Async Workers (Gevent and ASGI)

    master

    For applications using async I/O patterns, you can use specific worker classes.

    Gevent

    Requires the gevent extra and Python development headers (e.g., sudo apt-get install python3-dev on Ubuntu).

    ASGI (asyncio)

    No extra installation required. For improved performance, it is recommended to install and use uvloop.

    # Gevent
    pip install gunicorn[gevent]
    gunicorn app:app --worker-class gevent
    
    # ASGI (asyncio)
    gunicorn app:app --worker-class asgi
    
    # ASGI with uvloop (Recommended for performance)
    pip install uvloop
    gunicorn app:app --worker-class asgi --asgi-loop uvloop
  8. Use the official Gunicorn Docker image

    master

    Gunicorn provides an official Docker image on the GitHub Container Registry. You can pull the latest image or specific versions using the following tags:

    • ghcr.io/benoitc/gunicorn:latest - Latest release
    • ghcr.io/benoitc/gunicorn:24.1.0 - Specific version
    • ghcr.io/benoitc/gunicorn:24.1 - Minor version
    • ghcr.io/benoitc/gunicorn:24 - Major version
    docker pull ghcr.io/benoitc/gunicorn:latest