问题描述
为什么不起作用:
try:
与asyncio.wait_for(aiohttp.get( url),2)as resp:
print(resp.text())
除了asyncio.TimeoutError如e:
pass
送礼
与asyncio.wait_for(aiohttp.get( url),2)as resp:
AttributeError:__aexit__
据我了解, asyncio.wait_for()
将超过 aiohttp.get()
的未来,后者具有 __ aenter__
和 __ aexit __
方法(事实证明与aiohttp.get()async异步) >作品)。
您不能使用wait_for(...)$ c编写 async $ c>-
wait_for
不支持异步上下文管理器。
我将添加将超时
类设置为 asyncio
很快-请参见对话。
现在您可以尝试 aiohttp.Timeout
(尽管它需要安装足够胖的软件包)-或仅复制这40行代码。
有趣的是:这种方法不需要 UPD 我想念您已经使用aiohttp了。 Why wouldn't this work: Gives To my understanding, You cannot write I'll add For now you can try Interesting thing: the approach doesn't require UPD I missed that you use aiohttp already.Thus just follow the second example from aiohttp timeouts chapter. 这篇关于我可以使用asyncio.wait_for()作为上下文管理器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!与
异步-仅使用具有$code>的旧商品就足够了。 / p>
因此,请遵循中的第二个示例。 try:
async with asyncio.wait_for(aiohttp.get(url), 2) as resp:
print(resp.text())
except asyncio.TimeoutError as e:
pass
async with asyncio.wait_for(aiohttp.get(url), 2) as resp:
AttributeError: __aexit__
asyncio.wait_for()
would pass the future of aiohttp.get()
, which has an __aenter__
and __aexit__
method (as is demonstrated by the fact that async with aiohttp.get()
works).async with wait_for(...)
-- wait_for
doesn't support asynchronous context manager.Timeout
class to asyncio
soon -- see https://groups.google.com/forum/#!topic/python-tulip/aRc3VBIXyRc conversation.aiohttp.Timeout
(it requires installing a fat enough package though) -- or just copy these 40 lines of code.async with
-- just old good with
is enough.