如何让django频道消费者异步发送回复

how to make django channel comsumer to send reply asynchronously

在我的消费者中,我写了几个reply_channel.send

> def ws_message(message):    
>     line = get_output()
>     message.reply_channel.send({
>         "text": line,
>     })
>     line = get_output()
>     message.reply_channel.send({
>         "text": line,
>     })    line = get_output()
>     message.reply_channel.send({
>         "text": line,
>     })

而且我发现直到最后一条消息reply_channel.send执行完,消费者才发送这三个回复。

如何让它在每个 reply_channel.send 执行时立即发送回复?

我这样做是因为我有一个程序以随机​​间隔生成输出。如果我使用上面的代码,网页会一直等到完整的结果到达,这对用户来说很不友好。

谢谢!

将 immediate=True 传递给发送参数。