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

Wei*_*914 3 python asynchronous async-await python-asyncio aiohttp

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

我寻找了更多错误,但他们没有解决我的问题。我不太了解 await 函数或协程。我关闭了 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())
Run Code Online (Sandbox Code Playgroud)

我排除了它只是从api获取信息但出现错误-

RuntimeError: Timeout context manager should be used inside a task
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001774361A3C8>
Run Code Online (Sandbox Code Playgroud)

Ron*_*uya 6

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