FastAPI is a modern, high-performance, and robust Python web framework used for building REST APIs. It seamlessly integrates with UVICORN, a lightweight ASGI server, which excels in handling multiple connections and events concurrently. The choice of UVICORN for FastAPI is driven by its exceptional performance, ease of integration with FastAPI, and its ability to serve numerous incoming requests. In this article, we will delve deeper into UVICORN, exploring what it is and how it is employed in conjunction with FASTAPI
What is FastAPI?
FastAPI is a cutting-edge web framework designed for Python 3.8 and higher, utilizing standard Python type hints to create APIs. It is built on the foundations of Starlette and Pydantic, delivering several key advantages:
Some key features of FastAPI are:
- Very high performance.
- Faster to code
- Fewer bugs
- Easier to learn and use.
- Takes less time to read the docs.
What is Uvicorn?
UVICORN is an ASGI (Asynchronous Server Gateway Interface) web server implementation tailored for Python. Before ASGI, Python lacked a minimal low-level server interface for asynchronous frameworks. The ASGI specification acts as a bridge, enabling the creation of a common set of tools usable across all async frameworks. UVICORN currently provides support for HTTP/1.1 and WebSockets.
ASGI vs. WSGI
ASGI represents a significant advancement over WSGI (Web Server Gateway Interface). While WSGI is designed for single, synchronous applications that handle a request and return a response, it does not support long-lived connections like WebSocket connections. In contrast, ASGI is asynchronous and accepts three arguments:
- Scope: A Python dictionary containing details of the specific connection.
- Send: Allows the application to send event messages to the client.
- Receive: Permits the application to receive event messages from the client.
ASGI enables multiple incoming and outgoing events for each application simultaneously, allowing the application to remain responsive to user input while background coroutines run.
Command Line Options :
To access all available command line options for UVICORN, use the following command:
uvicorn --help
This will provide a comprehensive list of commands and options that can be used with UVICORN.
QuickStart with UVICORN and FastAPI :
Now that we understand why UVICORN is the preferred server for FASTAPI, let's take a quick look at how to start a FASTAPI application using UVICORN.
Step 1:
First, install the FASTAPI library and uvicorn library using following commands.
pip install fastapi
pip install uvicorn
Step 2:
Create a simple FASTAPI template in a file named main.py
that returns "Hello world" in JSON format.
Python
from typing import Union
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
Start the server using the following command:
uvicorn main:app --reload
Here, uvicorn main:app
refers to:
main
: The Python module in main.py
.app
: The FastAPI application object created within main.py
.--reload
: This flag instructs the server to automatically restart when code changes are detected, suitable for development purposes.
Step 3:
Open your browser and navigate to http://localhost:8000/docs to access the HTML page that showcases the API documentation. This page is automatically generated by FastAPI and makes it easy for developers to understand and interact with the API.
Output
UI
Similar Reads
Python Falcon - Uvicorn
Python-based libraries, Falcon and Uvicorn are two powerful tools that, when used together, form a robust framework for building high-performance web applications. Falcon is a minimalist web framework designed for building fast and efficient APIs. Uvicorn serves as an ASGI server, bringing asynchron
3 min read
FastAPI - Type Hints
The readability and maintainability of your codebase can be greatly improved by comprehending and utilizing type hints. Developers can identify potential bugs early in the development process and produce more dependable and effective applications by clearly defining the expected data types. In this
3 min read
Fast API - Gunicorn vs Uvicorn
In this article, we will explore Gunicorn and Uvicorn in the context of FastAPI applications, examining two integral components essential for the deployment and execution of Python web services. What is FastAPI? FastAPI is a modern web framework specifically crafted for Python 3.8 and newer versions
3 min read
Return an Image in FastAPI
FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. Returning an image is a common requirement in web development, and FastAPI makes it straightforward to do. another famous framework for doing the same is Flask, which is also Python-based. In this article, we
4 min read
FastAPI in Containers - Docker
FastAPI is also an open-source Starlette-ASGI (Asynchronous Server Gateway Interface) micro-web framework written in Python used to create RESTful APIs. Docker is a set of platform-as-a-service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. Your p
4 min read
Top Computer Vision Models
Computer Vision has affected diverse fields due to the release of resourceful models. Some of these are the image classification models of CNNs such as AlexNet and ResNet; object detection models include R-CNN variants, while medical image segmentation uses U-Nets. YOLO and SSD models are perfect fo
10 min read
Feature Matching in OpenCV
OpenCV feature matching is a super cool technology in computer vision that's changing how machines understand the visual world. It's super important in things like image search, object recognition, image stitching, and making pictures look better. If you want to level up your image analysis, classif
8 min read
Run the fast-api server using Pycharm
FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. Returning an image is a common requirement in web development, and FastAPI makes it straightforward to do. another famous framework for doing the same is Flask, which is also Python-based. Pre-requisitePyChar
3 min read
Python | Bar Charts in Vincent
In this article, we will create bar charts with the help for vincent. Which is a library in python which does python to vega translation? It has the data capabilities of python and the visualization capabilities of javascript. It is built specifically for plotting Dataframes and series quickly. Requ
1 min read
Vectorization in Python
We know that most of the application has to deal with a large number of datasets. Hence, a non-computationally-optimal function can become a huge bottleneck in your algorithm and can take result in a model that takes ages to run. To make sure that the code is computationally efficient, we will use v
5 min read