Unleashing Next-Level ASGI Web Server Powerhouse
Uvicorn:
Empowering ASGI-Powered Python Web Servers with Enhanced Performance
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
- Uvicorn is an ASGI web server implementation for Python.
- 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.