When would you start a FastAPI app with `uvicorn` directly, or with Gunicorn and uvicorn-worker's `UvicornWorker`, instead of `fastapi run`?
answer
- fastapi run passes a subset
- the old reason for Gunicorn
- uvicorn.workers is deprecated
- -k uvicorn_worker.UvicornWorker
basics
~20 sUse fastapi run by default; call uvicorn directly when you need a Uvicorn option the FastAPI CLI does not pass through; use Gunicorn with uvicorn-worker's UvicornWorker only when you want Gunicorn's process manager, settings and hooks.
solid answer
~40 s`fastapi run` passes a fixed set of arguments to `uvicorn.run()`: host, port, reload and its directories, workers, root path, the proxy-header settings and its own log configuration. For anything else, such as `--limit-max-requests`, `--timeout-graceful-shutdown` or `--timeout-worker-healthcheck`, you run `uvicorn app.main:app` yourself. Gunicorn used to be required because Uvicorn could not restart dead workers; in FastAPI 0.143's setup (Uvicorn 0.54) it can, so FastAPI's docs see no reason to use the old Gunicorn-based image. You still choose `gunicorn app.main:app -w 4 -k uvicorn_worker.UvicornWorker` when a team already runs Gunicorn's settings and hooks or wants its pre-fork and `--preload`. Use the separate uvicorn-worker package: Uvicorn's own `uvicorn.workers` module is deprecated.
code
bash · 9 lines# Default: the FastAPI CLI
fastapi run app/main.py --workers 4
# Uvicorn directly, for options the CLI does not pass
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4 \
--limit-max-requests 10000 --limit-max-requests-jitter 1000
# Gunicorn as the process manager, Uvicorn inside each worker
gunicorn app.main:app -w 4 -k uvicorn_worker.UvicornWorker -b 0.0.0.0:8000go deeper
Know that fastapi run, uvicorn and Gunicorn with UvicornWorker all end with Uvicorn serving the FastAPI app.
Explain which options fastapi run passes to Uvicorn, why Gunicorn was once needed, and why the worker class now comes from the uvicorn-worker package.
Pick the launcher from the options and operations you need: request limits and shutdown timeouts via uvicorn, Gunicorn only for its settings, hooks and preload.
Standardise one launcher per platform and write down when a service may deviate, so worker restarts, timeouts and recycling behave the same across the fleet.
## Three ways to start the same app A FastAPI app is an ASGI application, and all three commands end up with **Uvicorn** serving it. They differ in who parses the options and who manages the processes. | Command | Who manages processes | Options you can set | |---|---|---| | `fastapi run app/main.py --workers 4` | Uvicorn's parent process | host, port, reload, workers, root path, proxy headers, forwarded IPs | | `uvicorn app.main:app --workers 4` | Uvicorn's parent process | every Uvicorn option | | `gunicorn app.main:app -w 4 -k uvicorn_worker.UvicornWorker` | the Gunicorn arbiter | Gunicorn's settings, part of them mapped into Uvicorn | ## `fastapi run` is a thin layer over Uvicorn The FastAPI CLI resolves the app, prints where it will be served, and calls `uvicorn.run()` with a fixed list of arguments: `host`, `port`, `reload`, `reload_dirs`, `workers`, `root_path`, `proxy_headers` and `forwarded_allow_ips`, plus its own log configuration when the output is a terminal. Everything else keeps Uvicorn's default. So you run Uvicorn directly when you need, for example: - `--limit-max-requests` and `--limit-max-requests-jitter`, which make a worker exit after a number of requests so that the supervisor replaces it, with jitter so that workers do not all restart together; - `--timeout-graceful-shutdown`, a ceiling on how long shutdown waits for open requests; - `--timeout-worker-healthcheck`, how long the parent waits for a worker to answer a ping (5 seconds by default); - `--limit-concurrency`, `--backlog`, or TLS options for serving HTTPS without a proxy. The command shape changes only slightly: Uvicorn takes an import string (`app.main:app`), does no app discovery, and binds `127.0.0.1` unless you pass `--host 0.0.0.0`. Like `fastapi run`, it reads `WEB_CONCURRENCY` when `--workers` is absent; Gunicorn's `-w` falls back to the same variable, then to 1. ## Why Gunicorn used to be the answer FastAPI's deployment docs explain the history. The old official image ran Uvicorn under Gunicorn because, at the time, **Uvicorn could not manage and restart dead worker processes**, so Gunicorn did that job. The image is now deprecated. Uvicorn's own parent process now: 1. restarts a worker that dies while serving; 2. kills a worker that stops answering health pings; 3. replaces workers one at a time on `SIGHUP`, and adds or removes one on `SIGTTIN` and `SIGTTOU`. With those built in, the docs conclude there is no reason to use a Gunicorn-based base image just to get workers; `--workers` is enough. ## When Gunicorn still earns its place Gunicorn is a mature process manager with its own configuration file, server hooks and settings. Reasons a team keeps it for a FastAPI app: - an existing fleet already configured, monitored and operated through Gunicorn; - `--preload`, which imports the app in the arbiter before forking workers (it does not make the lifespan run once; each worker still runs it); - Gunicorn's own worker timeout and graceful-timeout handling. Through `UvicornWorker`, several Gunicorn settings reach Uvicorn: `--keep-alive` becomes Uvicorn's keep-alive timeout, `--max-requests` plus a random share of `--max-requests-jitter` becomes Uvicorn's request limit, the worker timeout drives the heartbeat Uvicorn reports back to the arbiter, and `forwarded_allow_ips` is passed on. Other Uvicorn options have no Gunicorn equivalent; Uvicorn's docs say some, such as `--limit-concurrency`, are not supported this way. To change the event loop, HTTP implementation or lifespan mode, you subclass `UvicornWorker` and override its `CONFIG_KWARGS`. A `UvicornH11Worker` that uses Python's standard event loop and a pure-Python HTTP parser exists for runtimes where the compiled ones are unavailable. ## Use the right worker class Uvicorn still ships a `uvicorn.workers` module, but it is deprecated: importing it emits a `DeprecationWarning` that tells you to use the **uvicorn-worker** package (Python's default warning filters hide it, so a running server usually logs nothing about it), and Uvicorn's docs say it will be removed in a future release. Moving over changes only the package you install and the dotted path after `-k`; the Gunicorn settings and the app's import string stay as they were. The current command is: ```bash pip install gunicorn uvicorn-worker gunicorn app.main:app -w 4 -k uvicorn_worker.UvicornWorker -b 0.0.0.0:8000 ``` ## A sensible default - Start with `fastapi run`, with or without `--workers`. - Switch to `uvicorn` the day you need an option the CLI does not pass. - Choose Gunicorn for its operations model, not for worker restarts, which Uvicorn now handles itself. - Bind every launcher to `0.0.0.0` in a container; only `fastapi run` does that by default. - Expect the lifespan to run once per worker under all three; none of them shares startup work between processes.
- Does Gunicorn's `--preload` let a FastAPI app's lifespan run once for all workers?No. Preload imports the module in the Gunicorn arbiter before forking, but each `UvicornWorker` runs its own Uvicorn server, and that server sends the lifespan startup event in the worker. Startup code and lifespan-loaded resources therefore still run once per worker; only module-level work happens in the arbiter.
- What breaks if you keep `-k uvicorn.workers.UvicornWorker` from an old deployment?Nothing breaks yet in current Uvicorn: the module still works, and the `DeprecationWarning` it emits on import is hidden by Python's default warning filters, so the logs usually stay silent. Uvicorn's docs say it will be removed in a future release. Switch the class to `uvicorn_worker.UvicornWorker` and add the package to the image before an upgrade removes it.
- You need Gunicorn because Uvicorn cannot restart crashed workers
- fastapi run accepts every option that uvicorn accepts
- uvicorn.workers.UvicornWorker is the current class for Gunicorn
- Gunicorn's --preload makes the FastAPI lifespan run only once
- fastapi run --limit-max-requests is how you recycle FastAPI workers
Assumes FastAPI 0.143 with Uvicorn 0.54 and uvicorn-worker 0.4: Uvicorn's own supervisor restarts dead workers, and uvicorn.workers is deprecated in favour of the uvicorn-worker package.