Responding to concurrent requests with Flask and eventlet Responding to concurrent requests with Flask and eventlet flask flask

Responding to concurrent requests with Flask and eventlet


When you run app.run(debug=True) you are explicitly telling Flask to run your application on the development web server, which is based on Werkzeug. It does not matter that you have loaded eventlet.

If you want to run your application on the eventlet web server, you have to start an eventlet web server, which according to the documentation is started as follows:

wsgi.server(eventlet.listen(('', 8000)), your_app)

This is more or less what socketio.run() does in my Flask-SocketIO extension, with a little bit more complexity to optionally handle SSL. The lines of code that do this are: https://github.com/miguelgrinberg/Flask-SocketIO/blob/539cd158f49ce085151911cb63edbacd0fa37173/flask_socketio/init.py#L391-L408. If you look around those lines, you will see that there are three different start up chunks of code, one for werkzeug, one for eventlet and one for gevent. They are all different.


import eventleteventlet.monkey_patch()

Won't magically turn your code into a multithread beast that can handle request asynchronously (it is still pretty magical and awesome).

As you can see in this example, you need to launch the wsgi server using eventlet wsgi's implementation.

If you want a standard solution, have a look at how to use nginx and uwsgi to launch flask application. You also may be interested in the project Spawning that leverage the pain of creating a complete multithread wsgi handler.