0

我正在尝试编写一个脚本,该脚本从多个交易所异步返回货币值。该代码有效,但脚本返回的是协程对象和警告列表,而不是结果。

据我了解,gather 函数返回结果列表,但不是在这种情况下。

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

该脚本是对本网站代码的修改: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>
4

1 回答 1

-3

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

而不是import ccxt.async as ccxt它需要写成import ccxt

于 2018-02-28T00:03:29.030 回答