问题描述
我正在执行 aiohttp.ClientSession 实例的 request() ,有时会引发 asyncio.TimeoutError .我认为在这种情况下必须引发 aiohttp.ServerTimeoutError,它源自 asyncio.TimeoutError,正如该文档所说:http://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ServerTimeoutError为什么会发生?也许是因为我使用的是旧版本的 aiohttp?2.3.8
I am doing request() of aiohttp.ClientSession instance and sometimes asyncio.TimeoutError is raised. I thought that aiohttp.ServerTimeoutError must be raised in this cases, which derived from asyncio.TimeoutError, as this doc says: http://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ServerTimeoutErrorWhy is it happening? Maybe because I am using old version of aiohttp? 2.3.8
UPD 这可能发生在像这样的非常简单的代码中
UPD this may happen in very simple code like this
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
和 asyncio.TimeoutError
是不同类型的超时.
aiohttp.ServerTimeoutError
and asyncio.TimeoutError
are different types of timeout.
asyncio.TimeoutError
是一个一般的超时,可能由于许多不同的原因而发生,从不存在的域或太多的数据读取.
asyncio.TimeoutError
is a general timeout that can happen due to many different reasons starting from unexisting domain or too much data to read.
aiohttp.ServerTimeoutError
as search in aiohttp 源代码揭示用于仅一个地方 -当与服务器建立连接时,但从套接字读取某些内容需要太长时间.你也可以查看 aiohttp 查看实际情况,您会遇到 ServerTimeoutError
.
aiohttp.ServerTimeoutError
as search in aiohttp source code reveales is used in one place only - when connection with server established, but some reading from socket takes too long. You can also check aiohttp tests to see real situations, where you get ServerTimeoutError
.
网络请求的操作很复杂,可能会在很多不同的地方出错.不要试图理解它们(如果这不是你的目的).只要你只想做请求,抓住 TimeoutError
(因为 ServerTimeoutError
是一个 subclass) 以查看您是否应该更改 timeout
kwarg.
Operation of network request is complex and may go wrong in many different places. Don't try to understand them all (if that's not you purpose). As long as you just want to do request, catch TimeoutError
(since ServerTimeoutError
is a subclass) to see if you possibly should alter timeout
kwarg.
这篇关于为什么会引发 asyncio.TimeoutError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!