Lançado · em melhoria
Guia de FastAPI · 6/6
Por enquanto, este capítulo está disponível apenas em inglês.
A FastAPI application has no build step: you deploy it by running it on an ASGI server. This chapter covers running a production server with fastapi run and Uvicorn, choosing the number of worker processes, building a container image, and what to watch for behind a reverse proxy.
fastapi dev is for development; in production, use fastapi run. It turns off auto-reload and by default listens on port 8000 on all network interfaces (0.0.0.0).
# a single process
fastapi run app/main.py
# another port, with four worker processes
fastapi run app/main.py --port 80 --workers 4fastapi run uses Uvicorn under the hood. Running Uvicorn yourself gives the same result; in that case you point it at the app as module:variable instead of a file path.
uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4A single Python process effectively uses one CPU core, so to use several cores you start several processes with --workers. Start around the number of cores and tune with load testing. Each worker has its own memory, so keep an eye on memory use if your app loads large models.
If you deploy straight onto a server, use a process manager such as systemd so the app restarts after a reboot or a crash. On platforms like Kubernetes, where the cluster manages how many containers run, the usual approach is a single process per container and scaling by adding replicas.
Containers let you reproduce the runtime environment exactly. The approach recommended in the official docs starts from the official Python image, installs dependencies first and copies the code afterwards, so that changing only the code reuses the cached dependency layer.
FROM python:3-slim
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app
CMD ["fastapi", "run", "app/main.py", "--port", "80"]In a real project, pin the image tag to the same Python version you develop with. Write CMD in the exec (array) form shown above so that stop signals reach the app and your lifespan cleanup code actually runs.
docker build -t hello-fastapi .
docker run -d --name hello -p 8000:80 --env-file .env hello-fastapiKeep .env out of the image and supply it at run time with --env-file or your platform's secrets feature. Listing .venv, .env and __pycache__ in .dockerignore keeps the image small.
In production, a reverse proxy such as Nginx, Traefik or a cloud load balancer usually terminates HTTPS and forwards requests to the app. So the app can see the original client address and the https scheme, the proxy adds X-Forwarded-* headers, and the app server accepts those headers only from proxies it trusts.
server {
listen 443 ssl;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}uvicorn app.main:app --host 127.0.0.1 --port 8000 --proxy-headers --forwarded-allow-ips="127.0.0.1"If the proxy serves the app under a path prefix such as /api, tell FastAPI with FastAPI(root_path="/api") or the --root-path /api option so that /docs and generated links use the right URLs.
fastapi dev or --reload in production.FastAPI(docs_url=None, redoc_url=None)./health for load balancers and orchestrators.fastapi run or uvicorn, and set the process count with --workers.root_path correctly.For more detail, see the official documentation:
0 comentários
Fazer login · Faça login para deixar um comentário.
Seja o primeiro a comentar.