[Python-Dev] Adding timeout to socket.py and httplib.py

Josiah Carlson jcarlson at uci.edu
Tue Mar 20 23:11:02 CET 2007


"Alan Kennedy" <python-dev at alan.kennedy.name> wrote:
[snip]
> def create_connection(address, timeout=sentinel):
>   [snip]
>      if timeout != sentinel:
>        new_socket.settimeout(timeout)
>      if new_socket.gettimeout() == 0:
>        result = new_socket.connect_ex(address)
>      else:
>        new_socket.connect(address)
>        result = new_socket
>   [snip]
> 
> I know that this makes it all more complex, and I'm *not* saying the
> new function should be modified to include these concerns.
[snip]

But now the result could be either an error code OR a socket.  One of
the reasons to provide a timeout for the create_connection call, if I
understand correctly, is to handle cases for which you don't get a
response back in sufficient time.  If the user provides zero as a
timeout, then they may very well get an exception, which is what they
should expect.  Then again, even with an arbitrary timeout, an exception
is possible (especially if a host is down, etc.), and hiding the
exceptional condition (didn't connect in the allotted time) is a bad
thing.

 - Josiah



More information about the Python-Dev mailing list