Gunicorn (Green Unicorn) is a Python WSGI HTTP server for Unix. It's a pre-fork worker model HTTP server that serves as a robust and high-performance solution for deploying Python web applications in production environments. Gunicorn acts as an interface between your web application (written with frameworks like Flask, Django, Pyramid, etc.) and the web server (like Nginx or Apache, which typically sit in front of Gunicorn).
Its primary purpose is to receive HTTP requests and forward them to your Python web application, managing multiple concurrent connections efficiently. Gunicorn achieves this through its worker processes. When a Gunicorn master process starts, it forks several worker processes. Each worker process is responsible for handling incoming requests. This pre-fork model allows it to handle multiple requests concurrently without the complexities of multi-threading within the application itself, making it stable and resilient.
Key features of Gunicorn include:
- Lightweight and Fast: It has a small memory footprint and is designed for speed.
- Simple Configuration: Easy to set up and configure via command-line arguments, a configuration file, or environment variables.
- Worker Process Management: It intelligently manages its worker processes, including respawning dead workers.
- Various Worker Types: Supports different worker types (sync, eventlet, gevent, tornado) to cater to various concurrency models and application needs.
- WSGI Compliance: Fully compliant with the WSGI (Web Server Gateway Interface) specification, making it compatible with all major Python web frameworks.
- Production Ready: Designed for serving applications in production, often behind a reverse proxy like Nginx or Apache, which handle static files, SSL termination, and load balancing.
When you run `gunicorn your_app_module:your_app_object`, Gunicorn boots up, creates its workers, and starts listening for HTTP requests on a specified address and port, making your Python web application accessible over the network.
Example Code
python
app.py - A simple Flask application
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from Gunicorn and Flask!'
@app.route('/health')
def health_check():
return 'OK', 200
if __name__ == '__main__':
This block is for development purposes only,
Gunicorn will be used in production.
app.run(debug=True)
How to run this application with Gunicorn:
1. Install Flask and Gunicorn:
bash
pip install Flask gunicorn
2. Run Gunicorn:
Navigate to the directory containing `app.py` in your terminal and execute:
bash
gunicorn --workers 4 --bind 0.0.0.0:8000 app:app
- `--workers 4`: Specifies that Gunicorn should use 4 worker processes. A common recommendation is `(2 - CPU_CORES) + 1`.
- `--bind 0.0.0.0:8000`: Tells Gunicorn to listen on all available network interfaces on port 8000.
- `app:app`: This specifies the module (`app.py`) and the Flask application instance within that module (`app = Flask(__name__)`).
Explanation of the Gunicorn command:
After running the command, Gunicorn will start, you'll see log messages indicating that workers have been spawned and that it's listening on `http://0.0.0.0:8000`.
You can then open your web browser and go to `http://localhost:8000/` (or `http://127.0.0.1:8000/`) to see the 'Hello from Gunicorn and Flask!' message. You can also visit `http://localhost:8000/health` to see the health check endpoint.
To stop Gunicorn, press `Ctrl+C` in your terminal. This example demonstrates the basic usage of Gunicorn to serve a Flask application, which is a common deployment pattern for Python web services.








Gunicorn