【发布时间】:2022-09-27 21:50:34
【问题描述】:
我有一个进程,当它收到消息时,它会向 celery 进程发送命令。 从那里我想从芹菜工人发回一条消息给后端,告诉它“我现在完成了,你可以继续”。 那么我可以从外部向通道层发送群组消息吗
编辑可能还应该添加一些代码示例:
消费者.py
async def startGame(self):
if self.leader:
songList = await self.getSongs()
await self.downloadSongs(songList)
任务.py
@shared_task
def downloadSongs(songList, room_group_name):
from channels.layers import get_channel_layer
print(room_group_name)
for song in songList:
if not os.path.isfile(\"./songfiles/\" + song[\"song_id\"] + \".mp3\"):
print(\"Downloading song: \" + song[\"title\"])
channel_layer = get_channel_layer()
print(channel_layer)
async_to_sync(channel_layer.group_send)(
room_group_name,
{
\'type\': \'startGameGroup\',
})
print(\"Done sleeping\")
消费者.py
async def startGameGroup(self, event):
await self.send(text_data=json.dumps({
\"ContentType\": \"startGameGroup\",
}))
因此,当消费者运行 startGame 时,它会调用一个名为 downloadSongs 的 celery 任务。从那里它应该向后端发送一条消息,说它已经完成并且可以开始游戏了
-
请提供足够的代码,以便其他人可以更好地理解或重现该问题。
标签: python django django-channels