Gunicorn

Gunicorn is a pure Python WSGI server with simple configuration and multiple worker implementations for performance tuning.

  • It tends to integrate easily with hosting platforms.

  • It does not support Windows (but does run on WSL).

  • It is easy to install as it does not require additional dependencies or compilation.

  • It has built-in async worker support using gevent or eventlet.

This page outlines the basics of running Gunicorn. Be sure to read its documentation and use gunicorn --help to understand what features are available.

Installing

Gunicorn is easy to install, as it does not require external dependencies or compilation. It runs on Windows only under WSL.

Create a virtualenv, install your application, then install gunicorn.

  1. $ cd hello-app
  2. $ python -m venv venv
  3. $ . venv/bin/activate
  4. $ pip install . # install your application
  5. $ pip install gunicorn

Running

The only required argument to Gunicorn tells it how to load your Flask application. The syntax is {module_import}:{app_variable}. module_import is the dotted import name to the module with your application. app_variable is the variable with the application. It can also be a function call (with any arguments) if you’re using the app factory pattern.

  1. # equivalent to 'from hello import app'
  2. $ gunicorn -w 4 'hello:app'
  3. # equivalent to 'from hello import create_app; create_app()'
  4. $ gunicorn -w 4 'hello:create_app()'
  5. Starting gunicorn 20.1.0
  6. Listening at: http://127.0.0.1:8000 (x)
  7. Using worker: sync
  8. Booting worker with pid: x
  9. Booting worker with pid: x
  10. Booting worker with pid: x
  11. Booting worker with pid: x

The -w option specifies the number of processes to run; a starting value could be CPU * 2. The default is only 1 worker, which is probably not what you want for the default worker type.

Logs for each request aren’t shown by default, only worker info and errors are shown. To show access logs on stdout, use the --access-logfile=- option.

Binding Externally

Gunicorn should not be run as root because it would cause your application code to run as root, which is not secure. However, this means it will not be possible to bind to port 80 or 443. Instead, a reverse proxy such as nginx or Apache httpd should be used in front of Gunicorn.

You can bind to all external IPs on a non-privileged port using the -b 0.0.0.0 option. Don’t do this when using a reverse proxy setup, otherwise it will be possible to bypass the proxy.

  1. $ gunicorn -w 4 -b 0.0.0.0 'hello:create_app()'
  2. Listening at: http://0.0.0.0:8000 (x)

0.0.0.0 is not a valid address to navigate to, you’d use a specific IP address in your browser.

Async with gevent or eventlet

The default sync worker is appropriate for many use cases. If you need asynchronous support, Gunicorn provides workers using either gevent or eventlet. This is not the same as Python’s async/await, or the ASGI server spec. You must actually use gevent/eventlet in your own code to see any benefit to using the workers.

When using either gevent or eventlet, greenlet>=1.0 is required, otherwise context locals such as request will not work as expected. When using PyPy, PyPy>=7.3.7 is required.

To use gevent:

  1. $ gunicorn -k gevent 'hello:create_app()'
  2. Starting gunicorn 20.1.0
  3. Listening at: http://127.0.0.1:8000 (x)
  4. Using worker: gevent
  5. Booting worker with pid: x

To use eventlet:

  1. $ gunicorn -k eventlet 'hello:create_app()'
  2. Starting gunicorn 20.1.0
  3. Listening at: http://127.0.0.1:8000 (x)
  4. Using worker: eventlet
  5. Booting worker with pid: x