🚀 Gunicorn के साथ Production-ready API
जब हम Machine Learning model को FastAPI या Flask के साथ Web API में बदलते हैं, तो local development में uvicorn
या flask run
अच्छा काम करता है। लेकिन Production environment में हमें एक production-ready server की आवश्यकता होती है।
यहीं पर Gunicorn (Green Unicorn) का use किया जाता है। यह एक WSGI HTTP server है जो high-load handle कर सकता है और multiple workers के साथ scalable API deployment possible बनाता है।
🔹 Gunicorn क्या है?
Gunicorn एक Python WSGI HTTP server है जो आपकी application को production में run करता है। यह lightweight, simple और reliable है। इसे FastAPI (ASGI) या Flask (WSGI) frameworks के साथ integrate किया जा सकता है।
📌 Gunicorn vs Uvicorn
- Uvicorn: ASGI server है, development और async support के लिए बेहतर।
- Gunicorn: WSGI server है, production-level stability और scaling के लिए perfect।
- Best Practice: FastAPI के साथ Gunicorn + Uvicorn workers का combination।
⚡ Installation
pip install gunicorn uvicorn
📝 Step 1: Basic FastAPI App
एक simple FastAPI app example:
# main.py from fastapi import FastAPI app = FastAPI() @app.get("/") def home(): return {"message": "Hello, Production API!"}
📝 Step 2: Gunicorn से Run करना
Gunicorn server run करने के लिए:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app -b 0.0.0.0:8000
-w 4
→ 4 workers (CPU cores के अनुसार set करें)-k uvicorn.workers.UvicornWorker
→ ASGI support के लिए-b 0.0.0.0:8000
→ API को सभी network interfaces पर bind करना
📝 Step 3: Systemd Service Setup (Linux)
Production में stable service चलाने के लिए Gunicorn को systemd service बनाते हैं:
# /etc/systemd/system/mlapi.service [Unit] Description=Gunicorn instance to serve FastAPI ML API After=network.target [Service] User=ubuntu Group=www-data WorkingDirectory=/home/ubuntu/mlapi ExecStart=/home/ubuntu/mlapi/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app -b 0.0.0.0:8000 [Install] WantedBy=multi-user.target
फिर commands:
sudo systemctl start mlapi sudo systemctl enable mlapi sudo systemctl status mlapi
📦 Step 4: Docker + Gunicorn
Production में अक्सर Gunicorn को Docker container में run किया जाता है। Example Dockerfile:
FROM python:3.9 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["gunicorn", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "main:app", "-b", "0.0.0.0:8000"]
🚀 Gunicorn क्यों Production के लिए Best है?
- Multiple worker processes → अधिक load handle कर सकता है
- Scalable और stable
- Docker और Nginx के साथ easily integrate होता है
- Asynchronous support (Uvicorn workers के साथ)
🏆 निष्कर्ष
Gunicorn के साथ आप अपने Machine Learning APIs को Production-ready बना सकते हैं। यह scalability, reliability और performance provide करता है। यदि आप FastAPI/Flask app को deploy कर रहे हैं, तो Gunicorn + Uvicorn workers का combination सबसे बेहतर है।