Asyncio 新手 - 我正在尝试使用 ccxt 实现 asyncio,但收到此错误



我正试图从这里开始变得简单,这样我就可以掌握这些东西的窍门。我理解asyncio的前提,但尝试将其与ccxt一起使用会让人感到困惑。除了asyncio文档之外,还有什么推荐的资源可以转发给我吗?

import time
import ccxt.async_support as ccxt
import configpro
exchange = ccxt.coinbasepro({
'apiKey': configpro.apiKey,
'secret':configpro.secret,
'password': configpro.password,
'enableRateLimit': True})
async def test():
async with exchange as session:
print(await session.fetch_ticker('BTC/USD'))



async def main():

print(f"started at {time.strftime('%X')}")
await test()
print(f"finished at {time.strftime('%X')}")

asyncio.run(main())

我的错误:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001ED6BE8EDC0>
Traceback (most recent call last):
File "C:UsersmichaAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 116, in __del__
self.close()
File "C:UsersmichaAppDataLocalProgramsPythonPython39libasyncioproactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:UsersmichaAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 746, in call_soon
self._check_closed()
File "C:UsersmichaAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 510, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

这应该有效:

import time
import asyncio
import ccxt.async_support as ccxt
import configpro
print('CCXT Version:', ccxt.__version__)

async def test(exchange):
ticker = await exchange.fetch_ticker('BTC/USD')
pprint(ticker)

async def main():
exchange = ccxt.coinbasepro({
'apiKey': configpro.apiKey,
'secret':configpro.secret,
'password': configpro.password,
})
print(f"started at {time.strftime('%X')}")
await test(exchange)
print(f"finished at {time.strftime('%X')}")
await exchange.close()

asyncio.run(main())

查看此处的示例:

  • https://github.com/ccxt/ccxt/tree/master/examples/
  • https://github.com/ccxt/ccxt/tree/master/examples/py

最新更新