Есть вебсокет сервер на socketIO:
import eventlet import socketio sio = socketio.Server(logger=True) app = socketio.WSGIApp(sio) @sio.event def connect(sid, environ): logger.info(f'Client with id: {sid} was connected') @sio.event def message(sid, data): # logger.debug(data) print(data) @sio.event def notification(sid, data): to, msg = list(data.items()).pop() print(to, msg) sio.send(msg, to=to) @sio.event def disconnect(sid): logger.info(f'Client with id: {sid} was disconnected ')
И есть задача в celery:
from celery import Celery sio = socketio.Client() celery: Celery = Celery('tasks', broker='redis://:156489@localhost/0', backend='redis://:156489@localhost/0') @sio.event def connect(): print('connection established') sio.emit('message', {'message': 'Task is connected'}) @sio.event def disconnect(): print('disconnected from server') @sio.event def message(data): print('message received with ', data) sio.connect('http://localhost:5000', headers={'token': 'task'}) @celery.on_after_configure.connect def setup_periodic_tasks(sender, **kwargs): sender.add_periodic_task(10.0, send_notifications.s(), name='add every 10') @celery.task(name='send_notifications') def send_notifications(): logger.info('Search new unread notifications') print(sio.connected) sio.emit('notification', {'key': 'val'}) sio.send('from task')
Так вот сообщение в функции connect: sio.emit('message', {'message': ‘Task is connected’}) на сервер приходит, а те что в send_notifications, не приходят. Почему, не пойму.