Adding timeouts to Internet sockets

Timothy O'Malley timo at alum.mit.edu
Fri Dec 7 01:59:15 EST 2001


hola.

John Copella  wrote:
 > How have developers added timeout behavior to Internet sockets in Python?  I
 > am developing an application that uses ftplib, and I need to timeout if the
 > server is unavailable, etc.  I note that Tim O'Malley has a package out
 > (http://www.vex.net/parnassus/apyllo.py?i=87800233) that very cleverly and
 > transparently does this (or seems to).  Has anyone used it?  My application
 > will be mission-critical, and I would prefer to use whatever is considered
 > the current best practice.

It depends what you mean by "mission critical".

As I see it, timeoutsocket has one big weakness:  it doesn't help if the delay
results from your host name resolver.  If your DNS server is down, timeoutsocket
will not prevent your connection from hanging while it tries to resolve the host
name.  (Web servers have this problem, and the common practice is to disable
hostname resolution on incoming requests.)

In my experience, once the IP address is resolved, timeoutsocket works darn well.

I've received a bunch of emails from people who comment that timeoutsocket
fits their needs.  Most often, the emails read something like:  "Thank you
for  writing this. It is so simple to use and just what we needed."  I don't know
what types of applications all those people are using it for".

I'd be the last person to claim that widespread use means that this module is
suitable for your "mission critical" needs.  But, I will say that I haven't yet seen a
better solution to add timeouts on any and all TCP connections, including those
used by ftplib.

And if there was a better solution, I'd switch to using it.

Good Luck.

TimO




More information about the Python-list mailing list