[Python-Dev] Adding timeout to socket.py and httplib.py
facundo at taniquetil.com.ar
Tue Mar 20 15:45:55 CET 2007
Alan Kennedy wrote:
> I see that your updated socket.connect() method takes a timeout
> parameter, which defaults to None if not present, e.g.
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).
> import socket ; socket.setdefaulttimeout(10.0)
> how do I restore full blocking behaviour to a single socket? (a
> somewhat contrived case, I admit).
You can not, unless you have access to the socket object itself.
> If I have access to the socket object, then I can call
> "sock_obj.settimeout(None)", but in that case I don't need the new
> API. I could also do it with the call "sock_obj.setblocking(1)".
> If I don't have access to the socket object, i.e. I'm using timeouts
> indirectly through httplib/etc, then I'm stuck: there's no way I can
> change the blocking or timeout behaviour; back to square one.
No. This method is for easily do that job from higher level libraries.
The code that is in my patch, it's right now copied N times in higher
level libraries (httplib, ftplib, smtplib, etc).
In all those libraries, the socket is opened, used, and never changed
the state between non-blocking, timeout, and nothing.
Experience (personal and complains in mailing lists) shows that a
timeout is needed: a lot of times people wants to make
urllib2.urlopen(....., timeout=10), for example.
But never heard of anybody wanting to "go to timeout" and then "back to
blocking mode", with the same socket, using high level libraries.
> So the new proposed API partly addresses the problem of increasing
> control over the underlying socket, but doesn't address all cases. It
> specifically prevents setting a timeout value of None on a socket,
> which is an important use case, I think.
False. If you want to set a timeout value of None on a socket, you
surely can, I haven't touch any line of code in socket-the-object!
More information about the Python-Dev