Azure App Service for Linux with its Oryx build runner when identifies Python web apps run them using Gunicorn
, a WSGI server. So your startup script can have gunicorn command to spin up the FastAPI app with the help of Gunicorn's worker class uvicorn.workers.UvicornWorker
.
gunicorn -w 2 -k uvicorn.workers.UvicornWorker main:app
The -w
indicates the number of workers you want to spin up with Gunicorn as master.
Alternatively, you can make the number of workers to automatically be calculated using a configuration file provided as an argument to the gunicorn as shown below.
gunicorn --config gunicorn.py main:app
And the gunicorn.py can have logic that calculates the number of worker processes and other configuration arguments for gunicorn as shown below.
import multiprocessing
import os
name = "Gunicorn config for FastAPI"
accesslog = "/home/user/fastapi/gunicorn-access.log"
errorlog = "/home/user/fastapi/gunicorn-error.log"
bind = "0.0.0.0:8000"
worker_class = "uvicorn.workers.UvicornWorker"
workers = multiprocessing.cpu_count () * 2 + 1
Also there is a detailed step by step tutorial that demonstrates How to Deploy FastAPI on Azure App Service in just 30 minutes