我正在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]]

我几乎用了我在互联网上发现的类似问题的所有提示(我不会把它们写在下面,这样问题就不会看起来太大),然而,没有一个不适合我.你能告诉我怎么才能改掉这个错误吗?我已经完全放弃了

推荐答案

您可以在删除等待之后再试一试吗?

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] = exchange.markets.keys()
            except Exception as e:
                print(type(e).__name__, str(e))
            await exchange.close()
        return symbols_list

因为我在源代码中看到self.market是一个简单的词典

#https://github.com/ccxt/ccxt/blob/master/python/ccxt/async_support/base/exchange.py#L705

def set_markets(self, markets, currencies=None):
    values = []
    self.markets_by_id = {}
    # handle marketId conflicts
    # we insert spot markets first
    marketValues = self.sort_by(self.to_array(markets), 'spot', True)
    for i in range(0, len(marketValues)):
        value = marketValues[i]
        if value['id'] in self.markets_by_id:
            (self.markets_by_id[value['id']]).append(value)
        else:
            self.markets_by_id[value['id']] = [value]
        market = self.deep_extend(self.safe_market(), {
            'precision': self.precision,
            'limits': self.limits,
        }, self.fees['trading'], value)
        values.append(market)
    self.markets = self.index_by(values, 'symbol')

Python相关问答推荐

Altair -箱形图边界设置为黑色,中线设置为红色

使用Python和PRNG(不是梅森龙卷风)有效地生成伪随机浮点数在[0,1)中均匀?

如何使用entry.bind(FocusIn,self.Method_calling)用于使用网格/列表创建的收件箱

剧作家Python:expect(locator).to_be_visible()vs locator.wait_for()

Python中MongoDB的BSON时间戳

优化在numpy数组中非零值周围创建缓冲区的函数的性能

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

如何让 turtle 通过点击和拖动来绘制?

如何将ctyles.POINTER(ctyles.c_float)转换为int?

如何制作10,000年及以后的日期时间对象?

Pre—Commit MyPy无法禁用非错误消息

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

Django admin Csrf令牌未设置

搜索按钮不工作,Python tkinter

在代码执行后关闭ChromeDriver窗口

处理Gekko的非最优解

不允许 Select 北极滚动?

Tensorflow tokenizer问题.num_words到底做了什么?

什么是一种快速而优雅的方式来转换一个包含一串重复的列,而不对同一个值多次运行转换,