What is Gunicorn?
Gunicorn is a WSGI server As described in PEP3333, the Python Web Server Gateway Interface (WSGI) is a way to make sure that web servers and python web applications can talk to each other.
Gunicorn is built to facilitate multiple servers to interact with it, It also does not really care what you used to build your web application - as long as it can be interacted with using the WSGI interface.
Gunicorn takes care of everything which happens in-between the web server and your web application. This way, when coding up your a Django application you don’t need to find your own solutions for communicating with multiple web servers reacting to lots of web requests at once and distributing the load.
Three common building blocks when deploying a Python web application to production are:
- A web server: example Nginx
- A WSGI application server: example Gunicorn
- Your actual application: written using a developer-friendly framework like Django
How Gunicorn works
The web server accepts requests, takes care of general domain logic and takes care of handling https connections. Only requests which are meant to arrive at the application are passed on toward the application server and the application itself.
The application code does not care about anything except being able to process single requests.
The application server is what we’re talking about here. Let’s look into what it does.
To be more precise:
The server side invokes a callable object that is provided by the application side
So somewhere inside your application (usually a wsgi.py file) an object is defined which can be invoked by Gunicorn. This object is used to pass request data to your application, and to receive response data.
Gunicorns takes care of running multiple instances of your web application, making sure they are healthy and restart them as needed, distributing incoming requests across those instances and communicate with the web server. In addition to that, Gunicorn is very fast.
Gunicorn can be easily installed via pip:
pip install gunicorn
Check the Gunicorn application logs by typing:
sudo journalctl -u gunicorn