Michielvv
Michielvv

Reputation: 346

Can't get multiple uwsgi workers to work with flask-socketio

In development, flask-socketio (4.1.0) with uwsgi is working nicely with just 1 worker and standard initialization.

Now I'm preparing for production and want to make it work with multiple workers.

I've done the following:

Added redis message_queue in init_app:

socketio = SocketIO()
socketio.init_app(app,async_mode='gevent_uwsgi', message_queue=app.config['SOCKETIO_MESSAGE_QUEUE'])

(Sidenote: we are using redis in the app itself as well)

gevent monkey patching at top of the file that we run with uwsgi

from gevent import monkey
monkey.patch_all()

run uwsgi with:

uwsgi --http 0.0.0.0:63000 --gevent 1000 --http-websockets --master --wsgi-file rest.py --callable application --py-autoreload 1 --gevent-monkey-patch --workers 4 --threads 1

This doesn't seem to work. The connection starts rapidly alternating between a connection and 400 Bad request responses. I suspect these correspond to the ' Invalid session ....' errors I see when I enable SocketIO logging.

Initially it was not using redis at all,

redis-cli > PUBSUB CHANNELS *

resulted in an empty result even with workers=1.

it seemed the following (taken from another SO answer) fixed that:

# https://stackoverflow.com/a/19117266/492148
import gevent
import redis.connection
redis.connection.socket = gevent.socket

after doing so I got a "flask-socketio" pubsub channel with updating data.

but after returning to multiple workers, the issue returned. Given that changing the redis socket did seem to bring things in the right direction I feel like the monkeypatching isn't working properly yet, but the code I used seems to match all examples I can find and is at the very top of the file that is loaded by uwsgi.

Upvotes: 4

Views: 3349

Answers (2)

Miguel Grinberg
Miguel Grinberg

Reputation: 67492

You can run as many workers as you like, but only if you run each worker as a standalone single-worker uwsgi process. Once you have all those workers running each on its own port, you can put nginx in front to load balance using sticky sessions. And of course you also need the message queue for the workers to use when coordinating broadcasts.

Upvotes: 2

Michielvv
Michielvv

Reputation: 346

Eventually found https://github.com/miguelgrinberg/Flask-SocketIO/issues/535

so it seems you can't have multiple workers with uwsgi either as it needs sticky sessions. Documentation mentions that for gunicorn, but I did not interpret that to extend to uwsgi.

Upvotes: 1

Related Questions