What is a preferred production setup for Flask-SocketIO? Confused about Gunicorn as it can only be spawned with one worker
Asked Answered
C

1

11

From the deployment section in the documentation, Gunicorn can only work with 1 worker process with Flask-SocketIO. I was wondering what is the preferred way to deploy a flask-socket-io server? Currently I have a regular Flask app that uses a multi-worker gunicorn server that is proxy-passed to from nginx. While I don't have any load balancing, I expect concurrency to be taken care of by the multiple gunicorn workers, and not having that concerns me a little bit for the websockets server.

Maybe I misunderstand the way eventlets/greenlets function but I see uwsgi as the only other alternative that I have not explored. Is it worth gettinginto the learning curve of uwsgi for this purpose?

Cogon answered 7/9, 2020 at 5:48 Comment(0)
M
8

Both Gunicorn and uWSGI have a very limited load balancer that does not support the required sticky sessions.

If you want to use multiple workers with either of these frameworks, you need to start several single-worker servers on their own ports, and then use nginx in front as load balancer.

In addition to nginx, you need to add a message queue (RabbitMQ, Redis, etc) that all these processes can use to coordinate efforts.

See the documentation on deploying multiple servers here: https://flask-socketio.readthedocs.io/en/latest/deployment.html#using-multiple-workers

Minnie answered 7/9, 2020 at 16:33 Comment(3)
thanks for the quick answer Miguel, I had come across these steps in different reading orders, but my overall understanding of eventlets wasn't good enough for everything to click into place back then, the path seems pretty clear to me now :) also, Appreciate your work a lot!Cogon
What is the recommended solution for using multiple workers on Heroku?Graner
@JacekTrociński there is no specific recommendation for Heroku. The same general rules apply if you deploy on that platform.Minnie

© 2022 - 2024 — McMap. All rights reserved.