gevent.rst
This file
Path
docs/deploying/gevent.rst
80 lines · 2.4 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–35why, one step back1gevent2======34Prefer using :doc:`gunicorn` or :doc:`uwsgi` with gevent workers rather5than using `gevent`_ directly. Gunicorn and uWSGI provide much more6configurable and production-tested servers.78`gevent`_ allows writing asynchronous, coroutine-based code that looks9like standard synchronous Python. It uses `greenlet`_ to enable task10switching without writing ``async/await`` or using ``asyncio``.1112:doc:`eventlet` is another library that does the same thing. Certain13dependencies you have, or other considerations, may affect which of the14two you choose to use.1516gevent provides a WSGI server that can handle many connections at once17instead of one per worker process. You must actually use gevent in your18own code to see any benefit to using the server.1920.. _gevent: https://www.gevent.org/21.. _greenlet: https://greenlet.readthedocs.io/en/latest/222324Installing25----------2627When using gevent, greenlet>=1.0 is required, otherwise context locals28such as ``request`` will not work as expected. When using PyPy,29PyPy>=7.3.7 is required.3031Create a virtualenv, install your application, then install ``gevent``.3233.. code-block:: text3435 $ cd hello-appMerge pull request #4947 from pallets/pyprojectDavid Lordbefore adoption78b42b9
lines 36–37why, one step back36 $ python -m venv .venv37 $ . .venv/bin/activateMerge branch '2.1.x'David Lordbefore adoption9139af1
lines 38–80why, one step back38 $ pip install . # install your application39 $ pip install gevent404142Running43-------4445To use gevent to serve your application, write a script that imports its46``WSGIServer``, as well as your app or app factory.4748.. code-block:: python49 :caption: ``wsgi.py``5051 from gevent.pywsgi import WSGIServer52 from hello import create_app5354 app = create_app()55 http_server = WSGIServer(("127.0.0.1", 8000), app)56 http_server.serve_forever()5758.. code-block:: text5960 $ python wsgi.py6162No output is shown when the server starts.636465Binding Externally66------------------6768gevent should not be run as root because it would cause your69application code to run as root, which is not secure. However, this70means it will not be possible to bind to port 80 or 443. Instead, a71reverse proxy such as :doc:`nginx` or :doc:`apache-httpd` should be used72in front of gevent.7374You can bind to all external IPs on a non-privileged port by using75``0.0.0.0`` in the server arguments shown in the previous section. Don't76do this when using a reverse proxy setup, otherwise it will be possible77to bypass the proxy.7879``0.0.0.0`` is not a valid address to navigate to, you'd use a specific80IP address in your browser.