Asyncio returns corutine objects instead of results ccxt

  • 0
  • Pump00:00

I am trying to write a script which returns currency value from multiple exchanges asynchronously. The code works, but instead of results the script returns list of coroutine objects and warnings.

As I understand the gather function returns list of results, but not in that case.

Please, explain me why the gathering function cannot return results properly.

The script is modification of the code found at this website: http://skipperkongen.dk/2016/09/09/easy-parallel-http-requests-with-python-and-asyncio/

Here is the code:

import asyncio
import concurrent.futures
import os
import sys
import ccxt.async as ccxt
import pprint

sys.path.append('D:OneDriveFilesprogramowaniePhytoncryptoBot')
from exchanges import lista

pp = pprint.PrettyPrinter()

lista = ['binance','bitfinex', 'bitstamp', 'gdax', 'kraken', 'poloniex']
length = len(lista)

async def main():
    with concurrent.futures.ThreadPoolExecutor(max_workers=length) as executor:
        loop = asyncio.get_event_loop()

        futures = []
        for exchange_name in lista:
            exchange = getattr(ccxt, exchange_name)()
            futures.append(loop.run_in_executor(executor, exchange.fetch_ticker, 'LTC/BTC'))

        for i in lista:
           results = await asyncio.gather(*futures)

        pp.pprint(results)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

Results:

[<coroutine object binance.fetch_ticker at 0x05F1B900>,
 <coroutine object bitfinex.fetch_ticker at 0x06268BA0>,
 <coroutine object bitstamp.fetch_ticker at 0x062705A0>,
 <coroutine object gdax.fetch_ticker at 0x06277E10>,
 <coroutine object kraken.fetch_ticker at 0x062827B0>,
 <coroutine object poloniex.fetch_ticker at 0x0628A090>]

Warns:

C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'poloniex.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'kraken.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'gdax.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'bitstamp.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'bitfinex.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:UsersDanielAppDataLocalProgramsPythonPython36-32libasynciobase_events.py:1427: RuntimeWarning: coroutine 'binance.fetch_ticker' was never awaited
   handle = None  # Needed to break cycles when an exception occurs.
D:OneDriveFilesprogramowaniePhytonUdemyvenvlibsite-packagesccxtbaseexchange.py:238: RuntimeWarning: coroutine 'ClientSession.close' was never awaited
  self.session.close()
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x05F16130>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x05F167D0>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0626C050>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0626C750>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x062814B0>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x0626CE10>
Default Asked on December 20, 2019 in Programming,   Python.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.