小编whi*_*ust的帖子

Python Websockets:客户端接收无限锁定

我正在学习如何使用websockets带有asyncio.

使用Websockets Getting Started示例,这里是我的服务器和客户端代码(都使用 运行在两个单独的控制台中python <script>

wsserver.py

import asyncio

import websockets

msg_queue = asyncio.Queue()


async def consumer_handler(websocket):
    global msg_queue
    while True:
        message = await websocket.recv()
        print("Received message {}".format(message))
        await msg_queue.put("Hello {}".format(message))
        print("Message queued")


async def producer_handler(websocket):
    global msg_queue
    while True:
        print("Waiting for message in queue")
        message = await msg_queue.get()
        print("Poped message {}".format(message))
        websocket.send(message)
        print("Message '{}' sent".format(message))


async def handler(websocket, path):
    print("Got a new connection...")
    consumer_task = asyncio.ensure_future(consumer_handler(websocket))
    producer_task = asyncio.ensure_future(producer_handler(websocket))

    done, pending …
Run Code Online (Sandbox Code Playgroud)

python websocket python-3.6

2
推荐指数
1
解决办法
2437
查看次数

标签 统计

python ×1

python-3.6 ×1

websocket ×1