Asnycio-RuntimeError:超时上下文管理器应在任务未封闭的客户端会话中使用

时间:2019-08-12 10:50:34

标签: python asynchronous async-await python-asyncio aiohttp

我正试图从我使用包装器的api中获得一些响应。

我寻找了更多错误,但他们没有解决我的问题。我对等待功能或协程了解不多。我关闭了is_asnyc,它起作用了,但是我需要该选项。所以我无法关闭它。

import clashroyale, asyncio
token = "my token"
cr = clashroyale.official_api.Client(token=token, is_async=True)

async def top():
    p = await cr.get_top_players()
    return p

topplayers = asyncio.run(top())

我只是为了从api获取信息而将其排除在外,但出现了错误-

RuntimeError: Timeout context manager should be used inside a task
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001774361A3C8>

1 个答案:

答案 0 :(得分:1)

尝试在async方法内移动客户端的初始化。它可能尝试使用异步超时上下文管理器,并且由于您不在任务内,因此它会失败