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

Alan Kennedy python-dev at alan.kennedy.name
Tue Mar 20 16:53:48 CET 2007


[Alan Kennedy]
>> I see that your updated socket.connect() method takes a timeout
>> parameter, which defaults to None if not present, e.g.

[Facundo Batista]
> I did NOT update a connect() method. I created a connect() function, in
> the module socket.py (there's also a connect() method in the socket
> object, but I didn't touch it).

Sorry, my mistake.

I realise now that you're creating a whole new function, dedicated to
the special (but extremely common) case of creating a fully connected
client socket. My fault for not realising that first off.

So, a question I would ask is: Is "connect" the right name for that function?
 - Will users get confused between the "connect" function and
socket.connect method? They are doing different things.
 - Will the naming give rise to the question "the socket-module-level
function connect() takes a timeout parameter, why doesn't the
socket-method connect() take a timeout parameter as well?"

Perhaps a better name might be "create_connected_client_socket", or
something equally descriptive?

Another question I would ask is: "How do I ensure that my newly
created connected client socket is in blocking mode, *without* making
any assumptions about the value of socket.getdefaulttimeout()?"

If the answer to this question is "you can't", then I would suggest a
function signature and implementation like this instead

def connect(address, **kwargs):
    [snip]
            if kwargs.has_key('timeout'):
                sock.settimeout(kwargs['timeout'])
    [snip]

This would of course mean that the user would have to explicitly name
the 'timeout' parameter, but that's a good thing in this case, IMO.

Regards,

Alan.


More information about the Python-Dev mailing list