Skip to content
Myr

gevent.rst

This file

Path

docs/deploying/gevent.rst

76 lines · 2.2 KiB

Last changed by

Merge branch 'stable'David Lordbefore adoption798e006

History of this file

Take it

1gevent2======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``. This is11not the same as Python's ``async/await``, or the ASGI server spec.1213gevent provides a WSGI server that can handle many connections at once14instead of one per worker process. See :doc:`/gevent` for more15information about enabling it in your application.1617.. _gevent: https://www.gevent.org/18.. _greenlet: https://greenlet.readthedocs.io/en/latest/192021Installing22----------2324When using gevent, greenlet>=1.0 is required. When using PyPy,25PyPy>=7.3.7 is required.2627Create a virtualenv, install your application, then install ``gevent``.2829.. code-block:: text3031    $ cd hello-app32    $ python -m venv .venv33    $ . .venv/bin/activate34    $ pip install .  # install your application35    $ pip install gevent363738Running39-------4041To use gevent to serve your application, write a script that imports its42``WSGIServer``, as well as your app or app factory.4344.. code-block:: python45    :caption: ``wsgi.py``4647    from gevent.pywsgi import WSGIServer48    from hello import create_app4950    app = create_app()51    http_server = WSGIServer(("127.0.0.1", 8000), app)52    http_server.serve_forever()5354.. code-block:: text5556    $ python wsgi.py5758No output is shown when the server starts.596061Binding Externally62------------------6364gevent should not be run as root because it would cause your65application code to run as root, which is not secure. However, this66means it will not be possible to bind to port 80 or 443. Instead, a67reverse proxy such as :doc:`nginx` or :doc:`apache-httpd` should be used68in front of gevent.6970You can bind to all external IPs on a non-privileged port by using71``0.0.0.0`` in the server arguments shown in the previous section. Don't72do this when using a reverse proxy setup, otherwise it will be possible73to bypass the proxy.7475``0.0.0.0`` is not a valid address to navigate to, you'd use a specific76IP address in your browser.