Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要python websockets使用者和生产者示例_Python_Websocket - Fatal编程技术网

需要python websockets使用者和生产者示例

需要python websockets使用者和生产者示例,python,websocket,Python,Websocket,包含以下示例: async def consumer_handler(websocket, path): while True: message = await websocket.recv() await consumer(message) 及 但是没有关于consumer()和producer()实现的示例或任何解释。有人能提供一个简单的例子吗?在第一个例子中,消费者处理程序侦听来自websocket连接的消息。然后它将消息传递给消费者。以最简单的形

包含以下示例:

async def consumer_handler(websocket, path):
    while True:
        message = await websocket.recv()
        await consumer(message)


但是没有关于
consumer()
producer()
实现的示例或任何解释。有人能提供一个简单的例子吗?

在第一个例子中,
消费者处理程序
侦听来自websocket连接的消息。然后它将消息传递给
消费者
。以最简单的形式,消费者可以如下所示:

async def consumer(message):
    # do something with the message 
    
async def producer():
    message = "Hello, World!"
    await asyncio.sleep(5) # sleep for 5 seconds before returning message
    return message

在第二个示例中,
producer\u handler
producer
接收消息并将其发送到websocket连接。制作人可以是这样的:

async def consumer(message):
    # do something with the message 
    
async def producer():
    message = "Hello, World!"
    await asyncio.sleep(5) # sleep for 5 seconds before returning message
    return message

非常感谢。我最近没有使用过
asyncio
,尤其是websockets。很明显:D