FastAPI - Uvicorn



Unlike the Flask framework, FastAPI doesn’t 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.

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)

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.

Instead of starting Uvicorn server from command line, it can be launched programmatically also.

Example

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)

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