Mitch Skiles
Mitch Skiles

Reputation: 93

SocketIO emit from Asynchronous Celery worker is not working

I am using Flask-SocketIO to create a real-time notification system. There is an external API server that calls the socketio server in a separate thread via an RPC. The method invoked by the RPC creates a Celery task that when consumed, calls a method that invokes socketio.emit(). However, the message doesn't seem to actually be sent as no message is received in the javascript client. My instinct tells me that as the Celery worker is running in a separate process, the socketio.emit() method being called is not sending to the connected client although the objects exist at the same place in memory. The server is running gevent and Celery is receiving and completing the tasks as seen by the logs. Further I have verified that socketio.emit() is being called by the Celery worker and I have verified that when the task is called directly, bypassing Celery, socketio works as expected. Any ideas for how to get socketio to communicate correctly when it is being referenced by a celery task in a separate process?

Upvotes: 7

Views: 1412

Answers (2)

xilixjd
xilixjd

Reputation: 55

Did you forget adding the message_queue ?

socketio.init_app(app, message_queue='redis://localhost:6379/0')

Upvotes: 2

Muntaser Ahmed
Muntaser Ahmed

Reputation: 4637

You can run Celery in multiprocessing or eventlet mode.

By default, Celery uses multiprocessing to set up a new process for a new worker. Eventlet uses threads, which I believe is what you want to use in this scenario since you want shared memory.

You may find this documentation useful.

Upvotes: 0

Related Questions