1

I am trying to use flask socket.io in docker with basic commands.

When i am running single worker of gunicorn everything is working fine but when i increase the workers, then on the client side it start giving 400 Bad Request and on server logs i see Invalid session lWmxdNRmai59bRfLAAAA (further occurrences of this error will be logged with level INFO).

these are the commands i am using gunicorn --worker-class geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 2 application:app -b 0.0.0.0:5000

and application.py file is

from gevent import monkey monkey.patch_all()
from flask import Flask from flask_socketio import SocketIO from flask import session 
app = Flask(__name__) 
app.config.from_object("settings.BaseConfig") 
socket_app = SocketIO(
    app,
    cors_allowed_origins="*",
    message_queue=settings.get_evn("CACHE_QUEUE_URL"),
    async_mode="gevent", 
)
socket_app.init_app(app, cors_allowed_origins="*")

I am using redis for message queue and have verified that the messaging queue url is correct

using aws load balancer for serving the request

1 Answers1

4

Multiple gunicorn workers aren't supported because the Gunicorn load balancing algorithm is incompatible with Socket.IO requirements.

From the documentation:

Due to the limited load balancing algorithm used by gunicorn, it is not possible to use more than one worker process when using this web server. For that reason, all the examples above include the -w 1 option.

The workaround to use multiple worker processes with gunicorn is to launch several single-worker instances and put them behind a more capable load balancer such as nginx.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Hi @miguel-grinberg thanks for quick reply. currently i am using two gunicorn tasks with nginx So is there any way to auto scale them? – Shobhit Bhatnagar Nov 29 '22 at 10:52
  • Do you mean to auto scale based on load? Neither nginx nor gunicorn do that, so no, not with these tools. You will need something that can do the autoscaling, like maybe Kubernetes. But in any case, each time there is a scaling up or down event you are going to lose some clients, as the connections are re-arranged across the updated number of servers. – Miguel Grinberg Nov 30 '22 at 10:56