我正在try 创建一个小型的异步应用程序,它基于GitHub here中运行良好的代码.我试着用这种方式稍微修改一下,以适应我的需要
main.py个
import asyncio
from kernel.init import Init
async def main():
init = Init()
await init.loadSymbols()
if __name__ == '__main__':
loop = asyncio.new_event_loop()
asyncio.run(main())
init.py个
import ccxt.async_support as ccxt
class Init:
async def loadSymbols(self):
symbols_list = {}
for exchange_id in ['poloniex', 'binance']:
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class()
try:
await exchange.load_markets()
symbols_list[exchange_id] = await exchange.markets.keys()
except Exception as e:
print(type(e).__name__, str(e))
await exchange.close()
return symbols_list
但是当我开始的时候,我得到了这个错误
TypeError对象DICT_KEYS不能在‘AWait’表达式中使用
当然,我搜索了很长一段时间,基于互联网上类似的例子(link 1,link 2),我试图修复我的代码. 一百零二
class Init:
async def loadSymbols(self):
symbols_list = {}
for exchange_id in ['poloniex', 'binance']:
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class()
try:
await exchange.load_markets()
input_coroutines = exchange.markets.keys()
symbols_list = await asyncio.gather(*input_coroutines, return_exceptions=True)
except Exception as e:
print(type(e).__name__, str(e))
await exchange.close()
return symbols_list
但在本例中,我得到了一个错误
类型异步错误.将来,需要协议值或可等待值
Attempt two个
input_coroutines = [list(exchange.markets.keys())[0]]
我几乎用了我在互联网上发现的类似问题的所有提示(我不会把它们写在下面,这样问题就不会看起来太大),然而,没有一个不适合我.你能告诉我怎么才能改掉这个错误吗?我已经完全放弃了