FastAPI deployment concepts related to ASGI servers and process managers
06-05-2025
Uvicorn version 0.30.0:
- https://www.uvicorn.org/release-notes/#0300-may-28-2024
- adds a new multiprocess manager and deprecates the
uvivorn.workers
module - this removes the need for gunicorn for process worker management - https://github.com/tiangolo/uvicorn-gunicorn-starlette-docker/blob/master/README.md
- adds a new multiprocess manager and deprecates the
ASGI servers and worker managers:
- installing FastAPI comes with the recommended production ASGI server (uvicorn) by default
- this comes from
fastapi-cli
- https://github.com/fastapi/fastapi-cli - when you install FastAPI with something like
pip install "fastapi[standard]"
you already getuvicorn[standard]
as well
- this comes from
Starting multiple workers:
- https://fastapi.tiangolo.com/deployment/server-workers/#multiple-workers
- you can use fastapi directly, or one of the ASGI servers
- note that not all commands and config are supported by fastapi-cli
- add ability to pass in more arguments to FastAPI CLI to match Uvicorn options - https://github.com/fastapi/fastapi-cli/discussions/13
Passing in config to uvicorn:
How many workers should you run?
- https://docs.gunicorn.org/en/stable/design.html#how-many-workers
- (2 x $num_cores) + 1
Uvicorn vs gunicorn:
- https://www.uvicorn.org/deployment/?h=workers#built-in
- uvicorn
- uses spawn worker model - https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
- gunicorn
- uses pre-fork worker model - https://docs.gunicorn.org/en/latest/design.html
- uvicorn inlcudes a gunicorn worker class
uvicorn.workers.UvicornWorker
- this will be deprecated in favour of the uvicorn-worker package -
# using uvloop and httptools
gunicorn -w 4 -k uvicorn.workers.UvicornWorker
# pure python implementation
gunicorn -w 4 -k uvicorn.workers.UvicornH11Worker
# uvicorn_worker module (from the uvicorn-worker package)
gunicorn example:app -w 4 -k uvicorn_worker.UvicornWorker
Local development with locally-trusted SSL certificates: