为什么会引发asyncio.TimeoutError?

问题描述 投票:2回答:1

我正在执行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()
python python-3.x python-asyncio aiohttp
1个回答
2
投票

aiohttp.ServerTimeoutErrorasyncio.TimeoutError是不同类型的超时。

asyncio.TimeoutError是一种常规超时,由于许多不同的原因,从未存在的域开始或者数据太多而无法读取。

aiohttp.ServerTimeoutError作为搜索aiohttp source code reveales用于one place only - 当与服务器建立连接时,但是从socket中读取的内容需要太长时间。您还可以查看aiohttp tests以查看真实情况,获得ServerTimeoutError

网络请求的操作很复杂,并且可能在许多不同的地方出错。不要试图全部了解它们(如果这不是你的目的)。只要你想做请求,赶上TimeoutError(因为ServerTimeoutErrorsubclass),看看你是否应该改变timeout kwarg。

© www.soinside.com 2019 - 2024. All rights reserved.