等待 ClientResponse.text() 中的代码 "crashing"

Code "crashing" in await ClientResponse.text()

我正在尝试创建一个 api,当我尝试将 aiohttp.ClientResponse 转换为文本时,我的代码永远不会完成(永远不会转到下一行)并且会引发 TimeoutError,我尝试在终端(使用同一站点)中执行此操作,并且有效,有人可以帮助我吗?

这是我当前的代码:

async with aiohttp.ClientSession() as session:
    # _base="https://frankerfacez.com"
    # query="monka"
    # sort="count-desc"
    r = await session.get(f'{_base}/emoticons/wall?q={query}&sort={sort}')

txt = await r.text()

它提出了这个:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 612, in run_until_complete
    return future.result()
  File "C:\Users\Kaigo\Desktop\FFZ Api\ffz\__init__.py", line 112, in search
    txt = await r.text()
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\client_reqrep.py", line 1009, in text
    await self.read()
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\client_reqrep.py", line 973, in read
    self._body = await self.content.read()
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\streams.py", line 358, in read
    block = await self.readany()
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\streams.py", line 380, in readany
    await self._wait('readany')
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\streams.py", line 296, in _wait
    await waiter
  File "C:\Users\Kaigo\AppData\Local\Programs\Python\Python38\lib\site-packages\aiohttp\helpers.py", line 596, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError

您正在尝试在关闭会话后读取请求。将 await r.text() 移动到 async with 块内:

async with aiohttp.ClientSession() as session:
    url = f'{_base}/emoticons/wall?q={query}&sort={sort}'
    async with session.get(url) as r:
        txt = await r.text()