Python / Uvicorn Fundamentals Interview Questions
What is the relationship between Uvicorn and FastAPI?
FastAPI and Uvicorn are complementary tools at different layers of the stack. FastAPI is the ASGI application framework; Uvicorn is the ASGI server that runs it. FastAPI generates the HTTP routing, request validation, and response serialisation; Uvicorn handles the network I/O, event loop, and connection lifecycle.
| Responsibility | FastAPI | Uvicorn |
|---|---|---|
| HTTP routing | Yes | No |
| Request validation (Pydantic) | Yes | No |
| Response serialisation | Yes | No |
| WebSocket routing | Yes | No |
| Network I/O (sockets) | No | Yes |
| Event loop management | No | Yes |
| Process management | No | Partial (--workers) |
| SSL termination | No | Yes (or reverse proxy) |
# main.py - FastAPI application from fastapi import FastAPI app = FastAPI() @app.get("/") async def root(): return {"message": "Hello World"} # Uvicorn runs the FastAPI app: # uvicorn main:app --reload # FastAPI is itself an ASGI-compliant application: # It implements: async def __call__(self, scope, receive, send) # Uvicorn calls this callable for every incoming request # Development: # uvicorn main:app --reload --host 127.0.0.1 --port 8000 # Production: # gunicorn main:app -w 4 -k uvicorn_worker.UvicornWorker
Installation: pip install fastapi uvicorn[standard] gives you both. FastAPI's documentation recommends Uvicorn as the server, and Uvicorn's documentation uses FastAPI in examples - they are tightly coupled in the ecosystem though architecturally independent (any ASGI framework can run on Uvicorn).
More Related questions...