How do I ensure a Django Channels message is sent immediately without delay?

我与影子孤独终老i 提交于 2019-12-07 12:54:31

问题


The idea is to run a background task on the worker.connect worker. While executing the task, I would like to send its progress to a connected client via the notifications Group.

The problem: the messages sent to the notifications Group are delayed until the task on the worker is finished. So: both messages 'Start' and 'Stop' appear simultaneously on the client, after a delay of 5 seconds (sleep(5)). I would expect the message 'Start', followed by a 5sec delay, followed by the message 'Stop'. Any idea why this is not the case?

I have the following three processes running:

  • daphne tests.asgi:channel_layer
  • python manage.py runworker --exclude-channel=worker.connect
  • python manage.py runworker --only-channel=worker.connect

In views.py:

def run(request, pk):
    Channel('worker.connect').send({'pk': pk})
    return HttpResponse(status=200)

In consumers.py:

def ws_connect(message):
    Group('notifications').add(message.reply_channel)
    message.reply_channel.send({"accept": True})

def worker_connect(message):
    run_channel(message)

In views.py:

def run_channel(message):
    Group('notifications').send({'text': 'Start'})
    sleep(5)
    Group('notifications').send({'text': 'Stop'})

routing.py

channel_routing = {
    'websocket.connect': consumers.ws_connect,
    'worker.connect': consumers.worker_connect,
}

回答1:


You can add immediately=True as argument to the send function. According to the source:

Sends are delayed until consumer completion. To override this, you may pass immediately=True.

https://github.com/django/channels/blob/master/channels/channel.py#L32



来源:https://stackoverflow.com/questions/41684282/how-do-i-ensure-a-django-channels-message-is-sent-immediately-without-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!