gunicorn.rst
This file
Path
docs/deploying/gunicorn.rst
130 lines · 3.9 KiB
Last changed by
Merge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
History of this fileTake it
Merge branch '2.1.x'David Lordbefore adoption9139af1
lines 1–32why, one step back1Gunicorn2========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.11* It has built-in async worker support using gevent or eventlet.1213This 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-appMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 33–34why, one step back33 $ python -m venv .venv34 $ . .venv/bin/activateMerge branch '2.1.x'David Lordbefore adoption9139af1
lines 35–130why, one step back35 $ 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.949596Async with gevent or eventlet97-----------------------------9899The default sync worker is appropriate for many use cases. If you need100asynchronous support, Gunicorn provides workers using either `gevent`_101or `eventlet`_. This is not the same as Python's ``async/await``, or the102ASGI server spec. You must actually use gevent/eventlet in your own code103to see any benefit to using the workers.104105When using either gevent or eventlet, greenlet>=1.0 is required,106otherwise context locals such as ``request`` will not work as expected.107When using PyPy, PyPy>=7.3.7 is required.108109To use gevent:110111.. code-block:: text112113 $ gunicorn -k gevent 'hello:create_app()'114 Starting gunicorn 20.1.0115 Listening at: http://127.0.0.1:8000 (x)116 Using worker: gevent117 Booting worker with pid: x118119To use eventlet:120121.. code-block:: text122123 $ gunicorn -k eventlet 'hello:create_app()'124 Starting gunicorn 20.1.0125 Listening at: http://127.0.0.1:8000 (x)126 Using worker: eventlet127 Booting worker with pid: x128129.. _gevent: https://www.gevent.org/130.. _eventlet: https://eventlet.net/