9

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?

fibonachoceres
  • 727
  • 4
  • 14

1 Answers1

6

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

Rick
  • 138
  • 1
  • 10
Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • 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! – fibonachoceres Sep 08 '20 at 04:58
  • What is the recommended solution for using multiple workers on Heroku? – Jacek Trociński May 16 '21 at 15:51
  • 1
    @JacekTrociński there is no specific recommendation for Heroku. The same general rules apply if you deploy on that platform. – Miguel Grinberg May 16 '21 at 16:11