Asyncio возвращает объекты corutine вместо результатов ccxt

Daniel спросил: 03 февраля 2018 в 10:11 в: python

Я пытаюсь написать скрипт, который асинхронно возвращает значение валюты из нескольких обменов. Код работает, но вместо результатов скрипт возвращает список объектов и предупреждений coroutine.

Как я понимаю, функция сбора возвращает список результатов, но не в этом случае.

Пожалуйста, , объясните мне, почему функция сбора не может правильно вернуть результаты.

Сценарий - это модификация кода, найденного на этом веб-сайте: http://skipperkongen.dk/2016/09/09/easy-parallel-http -requests-with-python-and-asyncio /

Вот код:

import asyncio
import concurrent.futures
import os
import sys
import ccxt.async as ccxt
import pprintsys.path.append('D:\OneDrive\Files\programowanie\Phyton\cryptoBot')
from exchanges import listapp = 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()

Результаты:

[<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>]
предупреждает:
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'poloniex.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'kraken.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'gdax.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'bitstamp.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'bitfinex.fetch_ticker' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
C:\Users\Daniel\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py:1427: RuntimeWarning: coroutine 'binance.fetch_ticker' was never awaited
   handle = None  # Needed to break cycles when an exception occurs.
D:\OneDrive\Files\programowanie\Phyton\Udemy\venv\lib\site-packages\ccxt\base\exchange.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>

0 ответов