3

我尝试从 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())

我收到此错误:

DeprecationWarning: There is no current event loop
  loop = asyncio.get_event_loop()

我使用 PyCharm 作为 IDE。

请问有哪位可以帮帮我吗?

4

2 回答 2

5

这是一个弃用警告,这意味着get_event_loop()您调用的函数的行为将很快改变,因为它将不再创建新的事件循环,而是 raise RuntimeErrorlike get_running_loop。从它的外观来看,预期的方法是显式地而不是隐式地创建一个新的事件循环。

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())

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

于 2021-10-26T19:52:07.437 回答
1

推荐的用于运行偶数循环的 api 是asyncio.run(在 Python 3.7 中引入)。

可以修改示例以采用它:


if __name__ == "__main__":
    asyncio.run(main())
于 2021-10-26T20:08:36.050 回答