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,
}
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