Asyncio返回corutine对象而不是结果ccxt

时间:2018-02-27 23:51:38

标签: python python-3.x python-asyncio ccxt

我正在尝试编写一个脚本,它以异步方式从多个交换中返回货币值。代码可以工作,但脚本不是结果,而是返回协程对象和警告的列表。

据我所知,收集函数返回结果列表,但不是那种情况。

请解释一下为什么收集功能无法正确返回结果。

该脚本正在修改本网站上的代码: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 pprint

sys.path.append('D:\OneDrive\Files\programowanie\Phyton\cryptoBot')
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()

结果:

[<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>

1 个答案:

答案 0 :(得分:-2)

经过两天的挣扎,我找到了理由。

而不是import ccxt.async as ccxt而是要求import ccxt代替。{/ p>