我正在使用 websocket 在 python 中发送和接收消息。我使用“websocket.send(msg)”以这些形式发送消息:
await ws.send(message)
Run Code Online (Sandbox Code Playgroud)
和
asyncio.run(ws.send(message))
Run Code Online (Sandbox Code Playgroud)
在 while 循环中,我首先检查连接是否处于活动状态,然后使用这些命令发送消息。在所有这些中,如果发送次数较少,没有问题,但是当发送次数增加时,我会收到发送消息的异常
Task <Task pending coro=<RunSocket() running at <ipython-input-1-b17eaf75a3de>:182> cb=[_run_until_complete_cb() at D:\Anaconda\InstallationFolder\lib\asyncio\base_events.py:158]> got Future <Future pending> attached to a different loop
Run Code Online (Sandbox Code Playgroud)
“请注意,RunSocket 是我的函数名称之一”
然后我得到这个错误:
got Future <Future pending> attached to a different loop
Run Code Online (Sandbox Code Playgroud)
我也尝试过这段代码:
asyncio.ensure_future(await ws.send(message))
Run Code Online (Sandbox Code Playgroud)
但它没有发送任何消息。谁能帮我解决这个错误?任何帮助将不胜感激。