1

I'm trying to start simple here just so I can get the hang of this stuff. I understand the premise of asyncio but its so confusing trying to use it with ccxt. Is there any recommended resources besides the asyncio documentation that someone can forward me to?

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())

And my error:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001ED6BE8EDC0>
Traceback (most recent call last):
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Users\micha\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39
  • Have you tried: https://stackoverflow.com/a/66772242/5750098 ? – Layne Bernardo Mar 02 '22 at 23:29
  • 1
    wow, thanks!! adding this like someone answered worked! ```asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())``` – Michael Padavona Mar 02 '22 at 23:49
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 03 '22 at 01:01
  • For more context, that was definitely a guess on my part but your exception is basically saying that asyncio is still trying to perform tasks (apparently deleting something) after the event loop has already ended. Not sure why this is but that's what pointed me in the direction of that answer. – Layne Bernardo Mar 03 '22 at 03:19

1 Answers1

1

This should work:

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())

Check the examples here:

Igor Kroitor
  • 1,548
  • 13
  • 16