2đź‘Ť
By the producer-consumer pattern, each consumer consumes the queue serially: get from queue > process > get from queue…
At the process phase for a given item (or “event”) you are putting the consumer to sleep, so the process is not finished until the sleep is done. You can’t consume the next item, that is why your consumer gets blocked.
You can make a new and dedicated consumer for that specific “event”, that way the sleep will not block the processing of other events. See asyncio.Queue, have your AsyncWebSocketConsumer
own an async queue and fill that with .put_nowait() and have a daemon coroutine (a new consumer) consume that with .get(), that new consumer can await asyncio.sleep(10)
before getting the next item.
Note that queue should have a carefully defined maxsize
, since it’s items are popped every 10 seconds letting it grow without limits may consume much memory. When que queue is full, put_nowait()
raises asyncio.QueueFull
, catch it in AsyncWebSocketConsumer
and just throw away the new item or find a way to tell the producer to slow down.
ps: I don’t know how django channels works, I have never used it. (#twisted tag is not appropriate for this question).