2

I’ve started working a lot with Flask SocketIO in Python with Eventlet and are looking for a solution to handle concurrent requests/threading. I’ve seen that it is possible with gevent, but how can I do it if I use eventlet?

danielo
  • 770
  • 2
  • 13
  • 32
  • I think you should look at deployment tools for flask, like `gunicorn`. There is more info on this matter in the flask-socketio docs page https://flask-socketio.readthedocs.io/en/latest/ You could set a number of workers there for concurrency – Andrew Che Feb 04 '18 at 18:28

2 Answers2

5

The eventlet web server supports concurrency through greenlets, same as gevent. No need for you to do anything, concurrency is always enabled.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thanks Miguel, good to know. Although,do you think the application would somehow benefit of using gunicorn in addition to eventlet as worker? And does eventlet support parallelism? – danielo Feb 05 '18 at 05:40
  • I wouldn't say there is a benefit. Eventlet comes with a greenlet based WSGI server, and gunicorn too. You can use either one with Flask-SocketIO. Eventlet, like gevent, runs in a single thread and uses cooperative multi-tasking. – Miguel Grinberg Feb 05 '18 at 07:04
-1

You could use gunicorn or its analogs to launch the app in production mode with several workers. As said here:

gunicorn --worker-class eventlet -w 5 module:app

Where the number after -w is the number of workers, module is your flask-socketio server module, and app is the flask app (app = flask.Flask(__name__)). Each worker is a process busy with handling incoming requests, so you will have concurrency. If the tasks your app does take significant time, the worker doing that task will be irresponsive while doing it.

Note: if you launch your app this way, the if __name__ == '__main__': part will be ignored, it seems that your module will be imported. And you don't need to call app.run yourself in the module in this case

Andrew Che
  • 928
  • 7
  • 20
  • 3
    I'm downvoting this because it is incorrect. You can't use more than one worker with gunicorn, because it lacks "sticky session" support. Sticky sessions are required for the Socket.IO protocol to work with multiple workers. The [documentation](https://flask-socketio.readthedocs.io/en/latest/#deployment) covers the valid deployment approaches for Flask-SocketIO applications. – Miguel Grinberg Feb 05 '18 at 05:04