← Back to list

Uvicorn and Gunicorn for FastAPI

Uvicorn is a fast ASGI server for Python, often used to run FastAPI and other ASGI-compatible web frameworks. Let’s break it down clearly:

Imran Khan · 2025-08-31 19:01 · 28 claps · 1.9 min read
#fastapi #python-fast-api #machine-learning #uvicorn #gunicorn
Open on Medium ↗
Wiki topics: ML · Machine Learning STP · Startups & Venture EDU · Education & Learning

Uvicorn and Gunicorn for FastAPI

Uvicorn is a fast ASGI server for Python, often used to run FastAPI and other ASGI-compatible web frameworks. Let’s break it down clearly:

  • Uvicorn stands for “Ultra Fast Python Web Server”.
  • ASGI server represents to Asynchronous Server Gateway Interface.
  • Uvicorn allows us to run running async Python frameworks like FastAPI, Starlette, etc.

Uvicorn is a Python ASGI server, similar to how Apache Tomcat runs Java apps or Node.js serves JavaScript apps — it handles HTTP requests and passes them to your application.

Note: Python is require prior to install Uvicorn as Uvicorn works on top of Python.

# Install Uvicorn
Install Uvicorn: pip install uvicorn

# main → Python file (main.py)
# app → FastAPI instance inside the file
Start Server: uvicorn main:app

# Start Server and automatic reload after change: 
uvicorn main:app - reload

# Specify Hose and Port: 
uvicorn main:app - host 0.0.0.0 - port 8000

# Enable Log: 
uvicorn main:app - reload - log-level info - access-log

# Set Log Level: 
uvicorn main:app - reload - log-level debug

# SSL / HTTPS: 
uvicorn main:app - ssl-keyfile key.pem - ssl-certfile cert.pem - host 0.0.0.0 - port 443

Deployment Commands:

Below are the deployment commands for dev and production:

// Start dev server on port 8000, automatic reload post changes or deployment and set log level as debug. DEV / QA Command: uvicorn main:app — reload — host 0.0.0.0 — port 8000 — log-level debug

// Start PROD server using Gunicorn with 4 unicorn workers on port 8000, automatic reload post changes or deployment. PROD Command: gunicorn -k uvicorn.workers.UvicornWorker main:app — workers 4 — bind 0.0.0.0:8000

Note:

  • Gunicorn is a WSGI/ASGI server for running Python web apps. Handles process management, load balancing, and concurrent requests.
  • -k specifies the worker class as uvicorn.workers.UvicornWorker tells Gunicorn to use Uvicorn as the worker. More workers means better concurrency and CPU utilization.

Worker

Separate process that handles incoming HTTP requests.

  • When a request comes in, Gunicorn passes it to one of its workers.
  • Each worker can handle one or more requests concurrently (depending on whether it’s sync or async).

Each worker runs its own copy of the FastAPI app. Number of workers =(2 x CPU cores) + 1 Example: 2 CPU cores → 2*2 + 1 = 5 workers

Note: Adding one worker always is as safey margin.

Work Importance:

  • Concurrency → Multiple workers allow handling many requests at the same time.
  • Fault tolerance → If one worker crashes, others keep serving requests.

Follow link to read more about Uvicorn.

I hope you found out this article interesting and informative. Please share it with your friends to spread the knowledge.

You can follow me for upcoming blogs follow. Thank you!


메타데이터
post_id
00a1ddb5ca4d
slug
uvicorn-for-fastapi-00a1ddb5ca4d
url
https://medium.com/@toimrank/uvicorn-for-fastapi-00a1ddb5ca4d
canonical_url
https://medium.com/@toimrank/uvicorn-for-fastapi-00a1ddb5ca4d
author_url
https://medium.com/@toimrank
status
ok
fetched_at
2026-07-17 20:28:52