Python 3.10 中的 event_loops 问题

Max*_*wer 8 python websocket python-asyncio binance python-3.10

我尝试从 Binance Websocket 获取数据。使用 python 3.9 作为解释器它运行良好,但使用 3.10 它给了我错误:(

这是代码:

import asyncio
from binance import AsyncClient, BinanceSocketManager


async def main():
    client = await AsyncClient.create()
    bm = BinanceSocketManager(client)
    # start any sockets here, i.e a trade socket
    ts = bm.trade_socket('BNBBTC')
    # then start receiving messages
    async with ts as tscm:
        while True:
            res = await tscm.recv()
            print(res)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()
Run Code Online (Sandbox Code Playgroud)

我使用 PyCharm 作为 IDE。

请问有人可以帮助我吗?

Jan*_*asa 10

这是一个弃用警告,这意味着您调用的函数的行为get_event_loop()很快就会改变,因为它将不再创建新的事件循环,而是引发RuntimeError类似get_running_loop. 从表面上看,预期的方法是显式而不是隐式创建一个新的事件循环。

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

我不确定是否asyncio.set_event_loop(loop)真的有必要。

  • 这里不需要`asyncio.set_event_loop(loop)`。 (2认同)