小编Kar*_*ouS的帖子

如何同时处理多个 websocket 消息?

如果有人可以帮助我使用 Python 和 async/await,任何帮助将不胜感激!

我需要监听 websocket 的消息,所以我设置了以下代码:

import websockets
import asyncio

my_socket = "ws://......."

# I set a "while True" here to reconnect websocket if it stop for any reason
while True:
    try:
        async with websockets.connect(my_socket) as ws:
            # I set a "while True" here to keep listening to messages forever
            while True:
                await on_message(await ws.recv())
    # If websocket gets closed for any reason, we catch exception and wait before new loop
    except Exception as e:
        print(e)
    # Wait 10 …
Run Code Online (Sandbox Code Playgroud)

python asynchronous async-await python-asyncio

5
推荐指数
1
解决办法
532
查看次数