Skip to content
Myr

gunicorn.rst

This file

Path

docs/deploying/gunicorn.rst

116 lines · 3.6 KiB

Last changed by

Merge branch 'stable'David Lordbefore adoption798e006

History of this file

Take it

The walk stopped after 200 commits: lines older than 73739a2 point at it.

Merge branch '2.2.x'David Lordbefore adoption73739a2

lines 1–10why, one step back
1Gunicorn
2========34`Gunicorn`_ is a pure Python WSGI server with simple configuration and5multiple worker implementations for performance tuning.67* It tends to integrate easily with hosting platforms.8* It does not support Windows (but does run on WSL).9* It is easy to install as it does not require additional dependencies10 or compilation.

Merge branch 'stable'David Lordbefore adoption798e006

line 11why, one step back
11* It has built-in async worker support using gevent.

Merge branch '2.2.x'David Lordbefore adoption73739a2

lines 12–95why, one step back
12
13This page outlines the basics of running Gunicorn. Be sure to read its14`documentation`_ and use ``gunicorn --help`` to understand what features15are available.1617.. _Gunicorn: https://gunicorn.org/18.. _documentation: https://docs.gunicorn.org/192021Installing22----------2324Gunicorn is easy to install, as it does not require external25dependencies or compilation. It runs on Windows only under WSL.2627Create a virtualenv, install your application, then install28``gunicorn``.2930.. code-block:: text3132 $ cd hello-app33 $ python -m venv .venv34 $ . .venv/bin/activate35 $ pip install . # install your application36 $ pip install gunicorn373839Running40-------4142The only required argument to Gunicorn tells it how to load your Flask43application. The syntax is ``{module_import}:{app_variable}``.44``module_import`` is the dotted import name to the module with your45application. ``app_variable`` is the variable with the application. It46can also be a function call (with any arguments) if you're using the47app factory pattern.4849.. code-block:: text5051 # equivalent to 'from hello import app'52 $ gunicorn -w 4 'hello:app'5354 # equivalent to 'from hello import create_app; create_app()'55 $ gunicorn -w 4 'hello:create_app()'5657 Starting gunicorn 20.1.058 Listening at: http://127.0.0.1:8000 (x)59 Using worker: sync60 Booting worker with pid: x61 Booting worker with pid: x62 Booting worker with pid: x63 Booting worker with pid: x6465The ``-w`` option specifies the number of processes to run; a starting66value could be ``CPU * 2``. The default is only 1 worker, which is67probably not what you want for the default worker type.6869Logs for each request aren't shown by default, only worker info and70errors are shown. To show access logs on stdout, use the71``--access-logfile=-`` option.727374Binding Externally75------------------7677Gunicorn should not be run as root because it would cause your78application code to run as root, which is not secure. However, this79means it will not be possible to bind to port 80 or 443. Instead, a80reverse proxy such as :doc:`nginx` or :doc:`apache-httpd` should be used81in front of Gunicorn.8283You can bind to all external IPs on a non-privileged port using the84``-b 0.0.0.0`` option. Don't do this when using a reverse proxy setup,85otherwise it will be possible to bypass the proxy.8687.. code-block:: text8889 $ gunicorn -w 4 -b 0.0.0.0 'hello:create_app()'90 Listening at: http://0.0.0.0:8000 (x)9192``0.0.0.0`` is not a valid address to navigate to, you'd use a specific93IP address in your browser.9495

Merge branch 'stable'David Lordbefore adoption798e006

lines 96–97why, one step back
96Async with gevent
97-----------------

Merge branch '2.2.x'David Lordbefore adoption73739a2

line 98why, one step back
98

Merge branch 'stable'David Lordbefore adoption798e006

lines 99–103why, one step back
99The default sync worker is appropriate for most use cases. If you need numerous,
100long running, concurrent connections, Gunicorn provides an asynchronous worker101using `gevent`_. This is not the same as Python's ``async/await``, or the ASGI102server spec. See :doc:`/gevent` for more information about enabling it in your103application.

Merge branch '2.2.x'David Lordbefore adoption73739a2

line 104why, one step back
104

Merge branch 'stable'David Lordbefore adoption798e006

line 105why, one step back
105.. _gevent: https://www.gevent.org/

Merge branch '2.2.x'David Lordbefore adoption73739a2

line 106why, one step back
106

Merge branch 'stable'David Lordbefore adoption798e006

lines 107–108why, one step back
107When using gevent, greenlet>=1.0 is required. When using PyPy, PyPy>=7.3.7 is
108required.

Merge branch '2.2.x'David Lordbefore adoption73739a2

lines 109–116why, one step back
109
110.. code-block:: text111112 $ gunicorn -k gevent 'hello:create_app()'113 Starting gunicorn 20.1.0114 Listening at: http://127.0.0.1:8000 (x)115 Using worker: gevent116 Booting worker with pid: x