Flask Socket-IO Emitting Across Google Cloud Run Container Instances #2013
-
I have been developing a web application that uses a flask web server, flask socket-io, and flask sessions. Previously I was deploying via a docker image to Google Cloud Run. Google Cloud Run increases the number of active containers based on traffic, and to allow for session permeance, I am newly deploying to the web with gunicorn, redis, eventlet. Working with redis and eventlet has raise a problem with emitting with flask socket-io however. The following message is logged when trying to emit:
This causes emitting to not work. Upon searching the web, I believe my problem is because Redis is not set up properly to allow the Google Cloud Run containers to emit to with one another. I've been playing with the configuration for days, but cannot seem to figure it out. Does anyone know why this issue is arising? I tried to follow the docs as closely as possible. Here is my docker file
The set up within my server.py file
The versions of redis, eventlet, flask-socketio, flask, and gunicorn are below.
Thank you all for your time. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I don't think you can mix threads and eventlet in the same worker. Try without the threads option. |
Beta Was this translation helpful? Give feedback.
Oh wow thank you so much for the help, really appreciate it.
I ended up trying redis memorystore from google cloud and it seems to have solved my problem. For anyone who comes across this in the future follow along here and use the following for the gunicorn command.
gunicorn --worker-class eventlet --bind :$PORT --workers 1 --timeout 0 server:app
Define socket IO like this:
socketio = SocketIO(app, message_queue=f'redis://{redis_host}:{redis_port}')
Lastly run the app like so:
socketio.run(app, host='127.0.0.1', port=8080, debug=False)
Leave the following the eventlet monkeypatch at the top of the server.py. Make sure you import redis in your server.py. See below: