engineering

Introduction to gunicorn

Gunicorn is a Python WSGI HTTP server for UNIX. It is a pre-fork worker model ported from Ruby's Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy[1]. Gunicorn implements the PEP3333 WSGI server standard specification so that it can run Python web applications that implement the application interface[2].

Installation

To install Gunicorn, you can use pip. Gunicorn requires Python 3.x >= 3.5[3]. To install the latest released version of Gunicorn, run the following command:

$ pip install gunicorn

You can also install Gunicorn from source just as you would install any other Python package:

$ pip install git+https://github.com/benoitc/gunicorn.git

Configuration

Gunicorn reads configuration information from five places. Gunicorn first reads environment variables for some configuration settings. Gunicorn then reads configuration from a framework-specific configuration file. Currently, this only affects Paster applications. The third source of configuration information is an optional configuration file gunicorn.conf.py searched in the current working directory or specified using a command-line argument. Anything specified in this configuration file will override any framework-specific settings. The fourth place of configuration information is command-line arguments stored in an environment variable named GUNICORN_CMD_ARGS. Lastly, the command-line arguments used to invoke Gunicorn are the final place considered for configuration settings. If an option is specified on the command line, this is the value that will be used[4].

Usage

To run a Gunicorn server, you need to specify the application module. The module name can be a full dotted path. The variable name refers to a WSGI callable that should be found in the specified module. For example, if you have a Flask application in a file named app.py with a WSGI callable named app, you can start the server with the following command:

$ gunicorn app:app

You can also specify the number of worker processes with the -w option:

$ gunicorn -w 4 app:app

Comparison with uWSGI

Gunicorn and uWSGI are both WSGI servers. They are both open-source tools, but Gunicorn is licensed under the MIT License, while uWSGI is licensed under the (L)GPL[5]. In many corporate environments, this is enough to mandate always using Gunicorn[5].

According to a comparison article, Gunicorn is easier to use and has better documentation than uWSGI. Gunicorn is also more lightweight and has a simpler architecture than uWSGI[6].

Additional Resources

Citations: [1] https://docs.gunicorn.org [2] https://www.fullstackpython.com/green-unicorn-gunicorn.html [3] https://docs.gunicorn.org/en/stable/install.html [4] https://docs.gunicorn.org/en/stable/configure.html [5] https://www.reddit.com/r/django/comments/or7yvi/is_it_better_to_use_gunicorn_or_uwsgi/ [6] https://medium.com/django-deployment/which-wsgi-server-should-i-use-a70548da6a83

Thoughts? Leave a comment