Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Python builds (pip, venv, requirements.txt)
How do you run a FastAPI application?
BeginnerHands-onPython builds (pip, venv, requirements.txt)
Answer
Short answer
FastAPI is an ASGI framework, so it runs on uvicorn. In development use uvicorn main:app --reload; in production run multiple workers with uvicorn --workers N or gunicorn with uvicorn workers. It also gives you free interactive API docs at /docs.
Example
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/orders/{order_id}")
async def get_order(order_id: int):
return {"id": order_id, "status": "SHIPPED"}
pip install fastapi "uvicorn[standard]"
uvicorn main:app --reload # development
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4 # production
Dockerfile CMD
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "2"]
Behind a proxy
Add --proxy-headers --forwarded-allow-ips="*" so the app sees the real client IP and https scheme from the load balancer.
What to say in the interview
"FastAPI runs on uvicorn. --reload for dev; multiple workers bound to 0.0.0.0 in production, or gunicorn managing uvicorn workers. /docs gives Swagger UI, and /health is used for readiness probes."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Python apps: pip, venv & requirements.txt
More on Python builds (pip, venv, requirements.txt)
How do you install dependencies and run a Python application?BeginnerWhat is a Python virtual environment and why do we need it?BeginnerWhat is requirements.txt and how do you pin versions?BeginnerHow do you run a Flask application in development and in production?BeginnerWhat is gunicorn and how many workers should you run?BeginnerWhat is the difference between WSGI and ASGI?Intermediate