为什么这个请求有这么高的延迟?

时间:2021-04-13 19:21:07

标签: python discord.py

我一直在尝试制作一个 Discord 机器人来获取有关加密货币的一些信息,但由于某种原因,请求需要 45 秒以上才能完成。

代码:

@bot.command()
async def rate(ctx, *args):
    t1 = perf_counter()
    if len(args) == 1:
        async with ctx.typing():
            coin = args[0]
            t2 = perf_counter()
            print(t2 - t1)
            async with aiohttp.ClientSession() as session:
                #try:
                if True:
                    async with session.get(f'https://api.coingecko.com/api/v3/simple/price?ids={coin}&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=true&include_last_updated_at=false', ssl=True) as r:
                        t2 = perf_counter()
                        print(t2 - t1)
                        r = await r.json()
                        #try:
                        if True:
                            embed = discord.Embed(title=f"{coin.capitalize()} rate", color=await color())
                            embed.add_field(name="Current price", value=f'${r[f"{coin}"]["usd"]}')
                            embed.add_field(name="24 Hour Change", value=f'{r[f"{coin}"]["usd_24h_change"]}%')
                            await ctx.send(embed=embed)
                        #except:
                            #embed = discord.Embed(title='Something went wrong...', description="The coin you provided was invalid. Make sure you don't enter the abbreviation, but the whole name. Example: `$rate bitcoin`", color=await color())
                            #await ctx.send(embed=embed)
                #except:
                    #embed = discord.Embed(title='Something went wrong...', description="Something went wrong on our side. It looks like we couldn't connect to a required API. Try again later.", color=await color())
                    #await ctx.send(embed=embed)
    else:
        embed = discord.Embed(title="An error occured", description="Make sure to specify one coin.", color=await color())
        await ctx.send(embed=embed)

我包含了一个计数器来检查经过的时间,并注释掉了除函数之外的错误,以便在控制台中显示错误。 这是我在控制台看到的;

0.17547020000000657
58.81905819999997

有没有办法解决这个问题?我需要使用其他请求库吗?

编辑 1:通过我的浏览器或 reqbin 发出请求不到一秒钟

0 个答案:

没有答案
相关问题