Python WebSockets消费者和生产者示例需求。

3
1个回答

1
在第一个例子中,consumer_handler 监听来自 websocket 连接的消息。然后将这些消息传递给 consumer。其最简单的形式如下:
async def consumer(message):
    # do something with the message 
    

在第二个示例中,producer_handler 接收来自 producer 的消息并将其发送到 WebSocket 连接。一个生产者可以看起来像这样:
async def producer():
    message = "Hello, World!"
    await asyncio.sleep(5) # sleep for 5 seconds before returning message
    return message

谢谢!我最近没有使用过asyncio和特定的Websockets。太明显了:D - shalakhin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接