eventlet

Prefer using Gunicorn with eventlet workers rather than using eventlet directly. Gunicorn provides a much more configurable and production-tested server.

eventlet allows writing asynchronous, coroutine-based code that looks like standard synchronous Python. It uses greenlet to enable task switching without writing async/await or using asyncio.

gevent is another library that does the same thing. Certain dependencies you have, or other considerations, may affect which of the two you choose to use.

eventlet provides a WSGI server that can handle many connections at once instead of one per worker process. You must actually use eventlet in your own code to see any benefit to using the server.

Installing

When using 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.

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

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

Running

To use eventlet to serve your application, write a script that imports its wsgi.server, as well as your app or app factory.

wsgi.py

  1. import eventlet
  2. from eventlet import wsgi
  3. from hello import create_app
  4. app = create_app()
  5. wsgi.server(eventlet.listen(("127.0.0.1", 8000)), app)
  1. $ python wsgi.py
  2. (x) wsgi starting up on http://127.0.0.1:8000

Binding Externally

eventlet 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 eventlet.

You can bind to all external IPs on a non-privileged port by using 0.0.0.0 in the server arguments shown in the previous section. Don’t do this when using a reverse proxy setup, otherwise it will be possible to bypass the proxy.

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