timeout in urllib.open()

John J. Lee jjl at pobox.com
Fri Feb 23 17:04:20 EST 2007


Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:

> Stefan Palme <kleiner at hora-obscura.de> writes:
> > is there a way to modify the time a call of
> > 
> >   urllib.open(...)
> > 
> > waits for an answer from the other side? Have a tool which
> > automatically checks a list of websites for certain content. The
> > tool "hangs" when one of the contacted websites behaves badly and
> > "never" answers...
> 
> Other than by using socket timeouts, at least in Un*x, you can also
> use signal.alarm.  You can only have one OS-provided alarm pending at
> a time, so if you want multiple overlapping timeouts you have to
> schedule them yourself with a single alarm.

Note that neither of these is guaranteed to time out urllib.open(),
though usually they will.

Another way to solve the underlying problem is not to try to time out
the network operation(s) at all, but to use threads or non-blocking
sockets (e.g. using Twisted).


John



More information about the Python-list mailing list