Hi,
I have been trying to port some legacy code from an old Python 2.7 codebase which used the requests library. In aiohttp.ClientSession, the constructor takes two timeout arguments: conn_timeout and read_timeout. However the ClientSession.request() only seems to take a single timeout argument. However the documentation on Timeouts do not seem to fully explain why there is this inconsistency between the constructor and the request() method. To quote:
Timeouts
By default all IO operations have 5min timeout. The timeout may be overridden by passing timeout parameter into ClientSession.get() and family:
...
None or 0 disables timeout check.
Note
Timeout is cumulative time, it includes all operations like sending request, redirects, response parsing, consuming response, etc.
Quoting the documentation on the ClientSession constructor:
read_timeout (float) – Request operations timeout. read_timeout is cumulative for all request operations (request, redirects, responses, data consuming). By default, the read timeout is 5*60 seconds. Use None or 0 to disable timeout checks.
conn_timeout (float) – timeout for connection establishing (optional). Values 0 or None mean no timeout.
So for example if I specify read_timeout and conn_timeout in the ClientSession constructor and then specify timeout in request() method, what happens to the conn_timeout and read_timeout? Which one takes precedence?
Finally, if I understand it correctly, when read_timeout is set to 5 minutes, then the whole request must finish within 5 minutes or it will timeout/AsyncCancelled? If my understanding is correct, then that means that even if a request is legitimately taking more than 5 minutes (maybe downloading a large file) it will cancel my download because of the timeout? Contrasting this with Python requests module:
timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds). If no timeout is specified explicitly, requests do not time out.
Highlighted part is important. As there are instances where we have to deal with small files that should not really take more than 5 seconds total to download but sometimes the remote is slow to respond in between bytes. So we set our timeout in the requests.get() method to 3 seconds so that latency TBB should not tie up the connection too long. However setting it to the same 3 seconds in aiohttp means that the download will timeout before it is completed. Is there a way to get the same kind of behavior in aiohttp.ClientSession as well?
Thanks in advance.