san*_*ash 10 python python-3.x python-asyncio aiohttp
我正在对 aiohttp.ClientSession 实例执行 request() ,有时会引发 asyncio.TimeoutError 。我认为在这种情况下必须引发 aiohttp.ServerTimeoutError ,它源自 asyncio.TimeoutError ,正如该文档所述: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()
Run Code Online (Sandbox Code Playgroud)
    aiohttp.ServerTimeoutError和asyncio.TimeoutError是不同类型的超时。
asyncio.TimeoutError是一种常见的超时,可能由于许多不同的原因而发生,从不存在的域或太多的数据读取。
aiohttp.ServerTimeoutError正如 aiohttp源代码中的搜索显示仅在一个地方使用  - 当与服务器建立连接时,但从套接字读取某些数据需要太长时间。您还可以检查 aiohttp测试以查看实际情况,您会在哪里得到ServerTimeoutError.
网络请求的操作比较复杂,很多地方都可能出错。不要试图理解所有这些(如果这不是您的目的)。只要你只想做请求,catch TimeoutError(因为ServerTimeoutError是一个子类)看看你是否应该改变timeoutkwarg。