Clarification in the documentation regarding ClientSession timeouts.

26 views
Skip to first unread message

nimrod...@gmail.com

unread,
May 25, 2018, 3:36:42 AM5/25/18
to aio-libs
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:

async with session.get('https://github.com', timeout=60) as r:
    ...
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.

Skorpeo Skorpeo

unread,
May 30, 2018, 1:33:42 PM5/30/18
to nimrod...@gmail.com, aio-libs
Just my two cents.  Not a big deal but I agree that timeout would make more sense to be after last receipt. I encountered this issue myself with large files, I just disable timeout currently and track it myself....

--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+unsubscribe@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/dd7cce62-d9e2-4bd3-9de0-f29a383d5a24%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Svetlov

unread,
May 30, 2018, 1:44:57 PM5/30/18
to aio-libs
Please wait for aiohttp 3.3 with reworked timeouts.
Or try github master ;)
Reply all
Reply to author
Forward
0 new messages