我正在对 aiohttp.ClientSession 实例执行 request() ,有时会引发 asyncio.TimeoutError 。我认为在这种情况下必须引发 aiohttp.ServerTimeoutError ,它派生自 asyncio.TimeoutError ,正如该文档所述:http://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.服务器超时错误 http://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ServerTimeoutError为什么会发生这种情况?也许是因为我使用的是旧版本的aiohttp? 2.3.8
UPD 这可能会发生在像这样的非常简单的代码中
async def example_of_code():
session = aiohttp.ClientSession()
response = await session.request(
method='POST',
url='some_url',
params={'some': 'params'},
data={'some': 'data'},
headers={'some': 'headers'},
timeout=10
)
return await response.json()
aiohttp.ServerTimeoutError
and asyncio.TimeoutError
是不同类型的超时。
asyncio.TimeoutError
是一种常见的超时,可能由于许多不同的原因而发生,从不存在的域或太多的数据读取。
aiohttp.ServerTimeoutError
作为 aiohttp 中的搜索源代码 https://github.com/aio-libs/aiohttp/Reveales 用于仅限一处 https://github.com/aio-libs/aiohttp/blob/7a324fd46ff7dc9bb0bb1bc5afb326e04cf7cef0/aiohttp/client_proto.py#L173- 当与服务器建立连接时,但从套接字读取某些数据花费的时间太长。您还可以检查 aiohttptests https://github.com/aio-libs/aiohttp/blob/c180800a4c90dc123d05311edbec92a3a82d6317/tests/test_client_functional.py#L2733看看真实的情况,你会得到什么ServerTimeoutError
.
网络请求的操作比较复杂,很多地方都可能出错。不要试图理解所有这些(如果这不是您的目的)。只要你只想做请求,catchTimeoutError
(since ServerTimeoutError
is a subclass https://docs.aiohttp.org/en/v2.3.0/client_reference.html)看看你是否应该改变timeout
kwarg.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)