Shobhit Bhatnagar
Shobhit Bhatnagar

Reputation: 71

Flask Socket - Invalid session lWmxdNRmai59bRfLAAAA (further occurrences of this error will be logged with level INFO)

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

Upvotes: 3

Views: 4970

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67489

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.

Upvotes: 8

Related Questions