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).
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
