我正在使用AsyncWebSocketConsumer
,并且想让消费者在处理一个特定事件时睡眠10秒钟。因此,在收到该事件后,我将呼叫await asyncio.sleep(10)
。但是问题在于,它会在接下来的10秒钟内阻止此消费者实例的事件。
我的问题是,如果我使用的是AsyncConsumer
,即使我处于异步睡眠状态,它也不应该继续发送将来的事件吗?
我一直在浏览daphne服务器的代码,发现以下内容:
def onmessage(self,payload,isBinary):
# If we're muted,do nothing.
if self.muted:
logger.debug("Muting incoming frame on %s",self.client_addr)
return
logger.debug("WebSocket incoming frame on %s",self.client_addr)
self.last_ping = time.time()
if isBinary:
self.application_queue.put_nowait(
{"type": "websocket.receive","bytes": payload}
)
else:
self.application_queue.put_nowait(
{"type": "websocket.receive","text": payload.decode("utf8")}
)
这意味着在从前端接收到新事件时,它将创建一个新任务并将其添加到异步队列中以供使用者使用。那么,为什么要睡在一个异步任务上阻止其他任务呢?我在这里想念东西吗?