`asyncio.TimeoutError` raised on network timeout and CPU timeout
etianen opened this issue · comments
A key difference between async-timeout
and asyncio.wait_for
is that wait_for
performs a final test that the task is completed before raising a TimeoutError
, whereas async-timeout
immediately raises the error when the timeout expires.
This difference is really important when a system is at 100% CPU usage.
Let's say you're making a HTTP request with a timeout of 15 seconds. The request takes 14.5 seconds to complete, but then the system hangs for a full second due to CPU contention.
async-timeout
will report a timeout error, since the timeout will be triggered immediately after the program un-freezes, but before the task completes.asyncio.wait_for
will not report a timeout error, since it will perform a final check to see if the network request completed before raising the error.
In other words, async-timeout
reports timeouts due to CPU and network, whereas wait_for
, only reports timeouts due to network.
The result being that, in a system that experiences a brief CPU spike, a great many network requests apear to "time out". The reality is that the python interpreter hung for a second, and the nework is fine.
The affects me when using aiohttp
. I've had to wrap the library with a shim that disables all built-in timeouts and wraps network calls with asyncio.wait_for
.
I don't think that your description is correct. The system can hang at any place, e.g. before finishing a request.
async-timeout doesn't spawn a task but adds a timer that cancels the current task. If it takes longer than delay -- TimeoutError is raised. That's it.