Quickstart¶
Get a Python web application running with Gunicorn in 5 minutes.
Install¶
pip install gunicorn
Create an Application¶
Create app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def hello():
return {"message": "Hello, World!"}
Django projects already have a WSGI application at myproject/wsgi.py.
No additional code is needed.
def app(environ, start_response):
data = b"Hello, World!"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return [data]
Run¶
gunicorn app:app
For Django:
gunicorn myproject.wsgi
For FastAPI (ASGI):
gunicorn app:app --worker-class asgi
Add Workers¶
Use multiple workers to handle concurrent requests:
gunicorn app:app --workers 4
A good starting point is 2 * CPU_CORES + 1 workers.
Bind to a Port¶
By default Gunicorn binds to 127.0.0.1:8000. Change it with:
gunicorn app:app --bind 0.0.0.0:8080
Configuration File¶
Create gunicorn.conf.py for reusable settings:
bind = "0.0.0.0:8000"
workers = 4
accesslog = "-"
Then run:
gunicorn app:app
Gunicorn automatically loads gunicorn.conf.py from the current directory.