5.2 Web Server
FastAPI does not have its own web server built-in, but it is designed to run on ASGI servers (Asynchronous Server Gateway Interface), primarily:
1. Common ASGI Servers for FastAPI
Section titled “1. Common ASGI Servers for FastAPI”| Server | Description |
|---|---|
| Uvicorn | Most commonly used. Lightweight and fast. |
| Hypercorn | Alternative ASGI server. Supports HTTP/2, QUIC. |
| Daphne | Part of Django Channels (less common for FastAPI). |
1.1 Typical Usage
Section titled “1.1 Typical Usage”You install and run Uvicorn like this:
pip install uvicornuvicorn main:app --reloadmain→ your Python file (i.e.,main.py)app→ the FastAPI instance (app = FastAPI())--reload→ auto-reload when code changes (for dev only)
1.2 Under the Hood
Section titled “1.2 Under the Hood”FastAPI is:
- Just a Python framework to define routes and logic.
- ASGI-compatible (similar to how Flask is WSGI-compatible).
- Dependent on an external ASGI server like Uvicorn to actually serve HTTP requests.