Uvicorn Worker for Gunicorn

0.4.0 · active · verified Thu Apr 09

uvicorn-worker provides an enhanced Uvicorn worker for Gunicorn, designed to replace the workers previously bundled directly within the `uvicorn` package. This allows for independent development, future compatibility, and continued improvements of Uvicorn's Gunicorn integration. The current version is 0.4.0, and it follows an as-needed release cadence, often coinciding with `uvicorn` updates.

Warnings

Install

Imports

Quickstart

To use `uvicorn-worker`, you need an ASGI application and Gunicorn. Create an ASGI app (e.g., `main.py`), then run Gunicorn specifying `uvicorn.workers.UvicornWorker` as the worker class. The `uvicorn-worker` package, when installed, provides the implementation for this worker path.

import uvicorn

async def app(scope, receive, send):
    assert scope['type'] == 'http'
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello from Uvicorn Worker!\n',
    })

# To run this with Gunicorn and uvicorn-worker:
# 1. Save the above code as `main.py`
# 2. Run in your terminal:
#    gunicorn main:app -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
# 3. Access at http://localhost:8000

view raw JSON →