Cha*_*och 7 django celery celerybeat django-channels
Django 2.1.1、Django 频道 2.1.3、芹菜 4.2.1
我已经在 Celery 中设置了一个任务,在任务结束时,我需要向客户端发送一个 websocket 消息。但是,永远不会发送 websocket 消息。没有抛出任何错误,它只是不发送。
我已经设置了一个使用 Redis 作为后端的通道层。从普通的 Django 视图执行此操作可以正常工作。但是当在 Celery 任务中运行时,它会将消息发送到 Channels,我可以看到 Channels 确实运行了下面我的consumers.py 代码中显示的代码,但客户端从未收到 websocket 消息。
任务.py
def import_job(self):
# (do task calculations, store in data dict)
message = {'type': 'send_my_data',
'data': json.dumps(thecalcs) }
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)('core-data', message)
Run Code Online (Sandbox Code Playgroud)
消费者.py
class AsyncDataConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.channel_group_name = 'core-data'
# Join the group
await self.channel_layer.group_add(
self.channel_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave the group
await self.channel_layer.group_discard(
self.channel_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data=None, bytes_data=None):
pass
# Receive message from the group
async def send_my_data(self, event):
text = event['data']
# Send message to WebSocket
await self.send(text_data=text)
Run Code Online (Sandbox Code Playgroud)
设置.py
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
Run Code Online (Sandbox Code Playgroud)
由于没有异常/错误,我完全不知道这个过程的哪一部分失败了。
send()? 是的这是Channels和Redis之间的问题吗?是渠道和客户端之间的问题吗?
事实证明,芹菜在任务期间吞下了我的代码中的异常。我需要实现更彻底的日志记录来捕获这些异常。
| 归档时间: |
|
| 查看次数: |
2905 次 |
| 最近记录: |