What is Gunicorn ?

Gunicorn, a WSGI server, can be used alongside Uvicorn to serve FastAPI applications, despite not being specifically designed for ASGI. Commonly paired with frameworks like Flask and Django, Gunicorn boasts a mature ecosystem and is favored for production deployments. However, its synchronous nature, processing one request at a time per worker process, necessitates combining it with Uvicorn for optimal utilization of asynchronous programming in FastAPI.

Example:

Python3




import uvicorn
from fastapi import FastAPI
 
app = FastAPI()
 
@app.get("/")
async def index():
   return {"message": "Hello World"}


run the below command

gunicorn main:app --reload --log-level info --workers 3 --bind 0.0.0.0:8000

Output:

Note: Gunicorn works in docker or linux os systems, in windows it might not work.

Message: Hello World!!!!

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.

Similar Reads

What is FastAPI?

FastAPI is a modern web framework specifically crafted for Python 3.8 and newer versions, leveraging standard Python-type hints for API creation. It is constructed upon the robust bases of Starlette and Pydantic, offering a range of notable advantages. The key features of FastAPI include:...

What is Uvicorn?

Uvicorn is the recommended lightweight ASGI server for FastAPI, optimized for asynchronous programming. Known for its exceptional performance in handling asynchronous code, it’s favored by FastAPI users for its user-friendly design suitable for both development and production environments. Uvicorn also offers a convenient –reload option, automatically restarting the server upon detecting code changes, proving particularly valuable in the development phase....

What is Gunicorn ?

...

Difference between Gunicorn vs Uvicorn

Gunicorn, a WSGI server, can be used alongside Uvicorn to serve FastAPI applications, despite not being specifically designed for ASGI. Commonly paired with frameworks like Flask and Django, Gunicorn boasts a mature ecosystem and is favored for production deployments. However, its synchronous nature, processing one request at a time per worker process, necessitates combining it with Uvicorn for optimal utilization of asynchronous programming in FastAPI....