- FastAPI - Discussion
- FastAPI - Useful Resources
- FastAPI - Quick Guide
- FastAPI - Deployment
- FastAPI - Mounting Flast App
- FastAPI - Middleware
- FastAPI - Mounting A Sub-App
- FastAPI - FastAPI Event Handlers
- FastAPI - Websockets
- FastAPI - Using GraphQL
- FastAPI - Using MongoDB
- FastAPI - SQL Databases
- FastAPI - Crud Operations
- FastAPI - CORS
- FastAPI - Dependencies
- FastAPI - Nested Models
- FastAPI - Response Model
- FastAPI - Header Parameters
- FastAPI - Cookie Parameters
- FastAPI - Uploading Files
- FastAPI - Accessing Form Data
- FastAPI - HTML Form Templates
- FastAPI - Static Files
- FastAPI - Templates
- FastAPI - Request Body
- FastAPI - Pydantic
- FastAPI - Parameter Validation
- FastAPI - Query Parameters
- FastAPI - Path Parameters
- FastAPI - Rest Architecture
- FastAPI - IDE Support
- FastAPI - Type Hints
- FastAPI - Uvicorn
- FastAPI - OpenAPI
- FastAPI - Hello World
- FastAPI - Introduction
- FastAPI - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
FastAPI - Uvicorn
Unpke the Flask framework, FastAPI doesn’t contain any built-in development server. Hence we need Uvicorn. It implements ASGI standards and is pghtning fast. ASGI stands for Asynchronous Server Gateway Interface.
The WSGI (Web Server Gateway Interface – the older standard) comppant web servers are not suitable for asyncio apppcations. 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 pbraries. 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 pbrary handles the http protocols.
The installation of Uvicorn as described earper will install it with minimal dependencies. However, standard installation will also install cython based dependencies along with other additional pbraries.
pip3 install uvicorn(standard)
With this, the WebSockets protocol will be supported. Also, PyYAML will be installed to allow you to provide a .yaml file.
As mentioned earper, the apppcation 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 cpent browser will be automatically refreshed. In addition, the following command-pne 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 exppcitly, 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 apppcation 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. |
Instead of starting Uvicorn server from command pne, it can be launched programmatically also.
Example
In the Python code, call uvicorn.run() method, using any of the parameters psted 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)
Now run this app.py as Python script as follows −
(fastapienv) C:fastapienv>python app.py
Uvicorn server will thus be launched in debug mode.
Advertisements