- FastAPI - Home
- FastAPI - Introduction
- FastAPI - Hello World
- FastAPI - OpenAPI
- FastAPI - Uvicorn
- FastAPI - Type Hints
- FastAPI - IDE Support
- FastAPI - Rest Architecture
- FastAPI - Path Parameters
- FastAPI - Query Parameters
- FastAPI - Parameter Validation
- FastAPI - Pydantic
- FastAPI - Request Body
- FastAPI - Templates
- FastAPI - Static Files
- FastAPI - HTML Form Templates
- FastAPI - Accessing Form Data
- FastAPI - Uploading Files
- FastAPI - Cookie Parameters
- FastAPI - Header Parameters
- FastAPI - Response Model
- FastAPI - Nested Models
- FastAPI - Dependencies
- FastAPI - CORS
- FastAPI - Crud Operations
- FastAPI - SQL Databases
- FastAPI - Using MongoDB
- FastAPI - Using GraphQL
- FastAPI - Websockets
- FastAPI - FastAPI Event Handlers
- FastAPI - Mounting A Sub-App
- FastAPI - Middleware
- FastAPI - Mounting Flast App
- FastAPI - Deployment
FastAPI Useful Resources
FastAPI - Uvicorn
Unlike the Flask framework, FastAPI doesnt contain any built-in development server. Hence we need Uvicorn. It implements ASGI standards and is lightning fast. ASGI stands for Asynchronous Server Gateway Interface.
The WSGI (Web Server Gateway Interface the older standard) compliant web servers are not suitable for asyncio applications. Python web frameworks (such as FastAPI) implementing ASGI specifications provide high speed performance, comparable to web apps built with Node and Go.
Uvicorn uses uvloop and httptools libraries. It also provides support for HTTP/2 and WebSockets, which cannot be handled by WSGI. uvloop id similar to the built-in asyncio event loop. httptools library handles the http protocols.
Installing Standard Version of Uvicorn
The installation of Uvicorn as described earlier will install it with minimal dependencies. However, standard installation will also install cython based dependencies along with other additional libraries.
pip3 install uvicorn[standard] Requirement already satisfied: uvicorn[standard] in d:\projects\python\myenv\lib\site-packages (0.40.0) Requirement already satisfied: click>=7.0 in d:\projects\python\myenv\lib\site-packages (from uvicorn[standard]) (8.3.1) Requirement already satisfied: h11>=0.8 in d:\projects\python\myenv\lib\site-packages (from uvicorn[standard]) (0.16.0) Requirement already satisfied: colorama>=0.4 in d:\projects\python\myenv\lib\site-packages (from uvicorn[standard]) (0.4.6) Collecting httptools>=0.6.3 (from uvicorn[standard]) Downloading httptools-0.7.1-cp314-cp314-win_amd64.whl.metadata (3.6 kB) Collecting python-dotenv>=0.13 (from uvicorn[standard]) Downloading python_dotenv-1.2.1-py3-none-any.whl.metadata (25 kB) Requirement already satisfied: pyyaml>=5.1 in d:\projects\python\myenv\lib\site-packages (from uvicorn[standard]) (6.0.3) Collecting watchfiles>=0.13 (from uvicorn[standard]) Downloading watchfiles-1.1.1-cp314-cp314-win_amd64.whl.metadata (5.0 kB) Collecting websockets>=10.4 (from uvicorn[standard]) Downloading websockets-15.0.1-py3-none-any.whl.metadata (6.8 kB) Requirement already satisfied: anyio>=3.0.0 in d:\projects\python\myenv\lib\site-packages (from watchfiles>=0.13->uvicorn[standard]) (4.12.0) Requirement already satisfied: idna>=2.8 in d:\projects\python\myenv\lib\site-packages (from anyio>=3.0.0->watchfiles>=0.13->uvicorn[standard]) (3.11) Downloading httptools-0.7.1-cp314-cp314-win_amd64.whl (88 kB) Downloading python_dotenv-1.2.1-py3-none-any.whl (21 kB) Downloading watchfiles-1.1.1-cp314-cp314-win_amd64.whl (287 kB) Downloading websockets-15.0.1-py3-none-any.whl (169 kB) Installing collected packages: websockets, python-dotenv, httptools, watchfiles Successfully installed httptools-0.7.1 python-dotenv-1.2.1 watchfiles-1.1.1 websockets-15.0.1
With this, the WebSockets protocol will be supported. Also, PyYAML will be installed to allow you to provide a .yaml file.
As mentioned earlier, the application is launched on the Uvicorn server with the following command −
uvicorn main:app reload
The --reload option enables the debug mode so that any changes in app.pywill be automatically reflected and the display on the client browser will be automatically refreshed. In addition, the following command-line options may be used −
| Sr.No | Command & Description |
|---|---|
| 1 |
--host TEXT Bind socket to this host. [default 127.0.0.1] |
| 2 |
--port INTEGER Bind socket to this port. [default 8000] |
| 3 |
--uds TEXT Bind to a UNIX domain socket. |
| 4 |
--fd INTEGER Bind to socket from this file descriptor. |
| 5 |
--reload Enable auto-reload. |
| 6 |
--reload-dir PATH Set reload directories explicitly, default current working directory. |
| 7 |
--reload-include TEXT Include files while watching. Includes '*.py' by default |
| 8 |
-reload-exclude TEXT Exclude while watching for files. |
| 9 |
--reload-delay FLOAT Delay between previous and next check default 0.25 |
| 10 |
-loop [auto|asyncio|uvloop] Event loop implementation. [default auto] |
| 11 |
--http [auto|h11|httptools] HTTP protocol implementation. [default auto] |
| 12 |
--interface auto|asgi|asgi|wsgi Select application interface. [default auto] |
| 13 |
--env-file PATH Environment configuration file. |
| 14 |
--log-config PATH Logging configuration file. Supported formats .ini, .json, .yaml. |
| 15 |
--version Display the uvicorn version and exit. |
| 16 |
--app-dir TEXT Look for APP in the specified directory default current directory |
| 17 |
--help Show this message and exit. |
Starting Uvicorn Server Programmatically
Instead of starting Uvicorn server from command line, it can be launched programmatically also.
main.py
In the Python code, call uvicorn.run() method, using any of the parameters listed above −
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def index():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
Output
Now run this main.py as Python script as follows −
py main.py INFO: Will watch for changes in these directories: ['D:\\Projects\\python\\myenv'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [21620] using WatchFiles INFO: Started server process [4772] INFO: Waiting for application startup. INFO: Application startup complete.
Uvicorn server will thus be launched in debug mode.