How to Use Uvicorn Workers in Production with FastAPI
To use
uvicorn workers in production with FastAPI, run uvicorn your_app:app --host 0.0.0.0 --port 8000 --workers 4. The --workers option starts multiple worker processes to handle requests concurrently, improving performance and reliability.Syntax
The basic syntax to run a FastAPI app with multiple uvicorn workers is:
uvicorn your_app:app: Runs the FastAPI app whereyour_appis the Python module andappis the FastAPI instance.--host 0.0.0.0: Makes the server accessible externally.--port 8000: Sets the port number.--workers 4: Starts 4 worker processes to handle requests concurrently.
bash
uvicorn your_app:app --host 0.0.0.0 --port 8000 --workers 4
Example
This example shows how to run a simple FastAPI app with 2 workers using uvicorn. It demonstrates how multiple workers can serve requests concurrently.
python
from fastapi import FastAPI import time app = FastAPI() @app.get("/slow") async def slow_endpoint(): time.sleep(3) # Simulate slow processing return {"message": "This was slow"} # Run this command in terminal: # uvicorn example:app --host 0.0.0.0 --port 8000 --workers 2
Common Pitfalls
1. Using --workers with reload: The --reload option is for development and does not work well with multiple workers. Avoid combining them.
2. Not using a process manager: In production, use a process manager like systemd or supervisor to keep workers running and restart on failure.
3. Overloading workers: Setting too many workers can exhaust CPU and memory. A good rule is 2-4 workers per CPU core.
bash
## Wrong way (development reload with workers): # uvicorn example:app --reload --workers 4 ## Right way (production): # uvicorn example:app --host 0.0.0.0 --port 8000 --workers 4
Quick Reference
| Option | Description | Example |
|---|---|---|
| --host | Set server host address | --host 0.0.0.0 |
| --port | Set server port | --port 8000 |
| --workers | Number of worker processes | --workers 4 |
| --reload | Auto reload on code changes (dev only) | --reload (avoid with workers) |
Key Takeaways
Use the --workers option with uvicorn to run multiple worker processes for FastAPI in production.
Avoid using --reload with multiple workers; it is meant for development only.
Choose the number of workers based on CPU cores to balance performance and resource use.
Use a process manager to keep uvicorn workers running reliably in production.
Run uvicorn with --host 0.0.0.0 to make your FastAPI app accessible externally.