Unleashing Next-Level ASGI Web Server Powerhouse

Uvicorn:

Empowering ASGI-Powered Python Web Servers with Enhanced Performance

Mamta GuptaπŸ¦„
4 min readDec 24, 2023

Uvicorn is an ASGI web server implementation for Python.

Wait, what is an ASGI ?

  • ASGI stands for Asynchronous Server Gateway Interface
  • An interface which allows communication between web server and Asynchronous web application and frameworks

Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we’re now able to start building a common set of tooling usable across all async frameworks.

and lets welcome the hero of today’s blog β€” UVICORN πŸŽ‰

Uvicorn

  1. Uvicorn is an ASGI web server implementation for Python.
  2. It can be utilized with help of command
$ pip install uvicorn[standard]
OR
$ pip install uvicorn

3. Uvicorn installs cython-based dependecy and optional-extras

This cython-based includes having uvloop which aim to enhance the performance of applications by handling I/O operations efficiently and enabling asynchronous programming. { Do you recall something called libuv in nodejs ? ifykyk}

Speaking of ASGI servers, the first framework that comes to mind, gaining significant popularity, is FastAPI.

FastAPI

  • FastAPI has been recently used framework from asynchronous web framework for building APIs with python 3.8+
  • Speaking of running fastAPI with uvicorn, lets have look at an example in brief for clarity
  • With fastAPI framework creating a web application is as fast as creating a simple main.py file having below content.
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
return {
"success": True,
"message": "Hello World"
}
  • This main.py will act as web application after running with the help of an command :
$ uvicorn main:app
  • An unpopular opinion could be creating virtual environment install the dependency inside it. In current example it is uvicorn, fastapi
  • I am using poetry which is again an open source tool for dependency management and packaging in Python.
  • If you’re looking forward to get handy on one of the dependency management tool, you can give it a try. One of the starter command can be,

FastAPI with Uvicorn

One of the basic command to make your webapp up and running on your localhost server can be done as,

$ uvicorn main:app --reload


Command Input: uvicorn main:app --reload
β”‚
└───┐
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Uvicorn Application β”‚
β”‚ (ASGI Server) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ β”‚ FastAPI Application β”‚
β”‚ └───│ (app) β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
Options
(--reload)

Let’s dive deep into few development friendly favorite uvicorn settings.

1)reload

Enable auto-reload. Uvicorn supports two versions of auto-reloading behavior enabled by this option. Like reloading with watchfiles, reloading without watchfiles (only *.py files).

$ uvicorn main:app - reload

2) host

Specifies the network interface to bind the server to (default is β€˜127.0.0.1’ or β€˜localhost’). For example: β€” host 0.0.0.0 to make the server accessible from all network interfaces

$ uvicorn main:app - reload - host 0.0.0.0

3) port

Bind to a socket with this port. Default: 8000

$ uvicorn main:app - reload - host 0.0.0.0 - port 3200

4) log-level: logging

Set the log level. Options: β€˜critical’, β€˜error’, β€˜warning’, β€˜info’, β€˜debug’, β€˜trace’. Default: β€˜info’.

$ uvicorn main:app - reload - host 0.0.0.0 - port 3200 --log-level debug

5) loop : event loop implementation

Overrides the default event loop policy (asyncio on Python 3.7+). It can be set to β€˜asyncio’ or β€˜uvloop’ for using the uvloop event loop. Example: β€” loop uvloop.

$ uvicorn main:app --loop uvloop

NOTE:

The main difference between the event loop policies asyncio and uvloop lies in their implementations and performance characteristics:

asyncio: This is the default event loop policy in Python 3.7 and later versions. It is a built-in module that provides infrastructure for writing single-threaded concurrent code using coroutines, tasks, and futures. asyncio uses the default event loop provided by Python and is designed to be portable and compatible across different platforms.

uvloop: It is a third-party event loop policy built on top of libuv, which is a high-performance asynchronous I/O library used by Node.js. uvloop aims to significantly improve the performance of asyncio-based applications by replacing the default event loop with a faster one built on libuv. It is known for its higher throughput and lower latency compared to the default asyncio event loop.

Overall, uvloop is often preferred in scenarios where high-performance asynchronous I/O operations are critical, such as in web servers, network-bound applications, or when dealing with large-scale concurrent tasks. However, for general-purpose applications or situations where compatibility is a priority, the default asyncio event loop remains a solid choice.

--

--

Mamta GuptaπŸ¦„

Byte-Sized Imperfections in a World of Perfection ✑️