0
0
Flaskframework~5 mins

Gunicorn for production serving in Flask

Choose your learning style9 modes available
Introduction

Gunicorn helps run your Flask app smoothly when many people use it at the same time. It makes your app ready for real-world use.

You want to make your Flask app available to many users at once.
You need a reliable way to run your app on a server.
You want better performance than the simple Flask development server.
You are preparing your app to be deployed on a cloud or VPS.
You want to handle multiple requests without crashing.
Syntax
Flask
gunicorn [OPTIONS] MODULE_NAME:APP_VARIABLE

MODULE_NAME is the Python file name without .py.

APP_VARIABLE is the Flask app object inside your module.

Examples
Runs the Flask app named app inside the app.py file with default settings.
Flask
gunicorn app:app
Runs with 4 worker processes and binds to all IPs on port 8000 for external access.
Flask
gunicorn -w 4 -b 0.0.0.0:8000 app:app
Runs and prints access logs to the console for easier debugging.
Flask
gunicorn --access-logfile '-' app:app
Sample Program

This simple Flask app returns a greeting. Running it with Gunicorn will serve it efficiently in production.

Flask
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, Gunicorn!'

# To run this app with Gunicorn, save as app.py and run:
# gunicorn app:app
OutputSuccess
Important Notes

Gunicorn is not for Windows; use it on Linux or macOS servers.

Use multiple workers (-w) to handle more users smoothly.

Always test your app with Gunicorn before deploying live.

Summary

Gunicorn runs Flask apps efficiently for many users.

Use the command gunicorn module:app to start.

Configure workers and ports to fit your server needs.