如果有人可以帮助我使用 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)