Alarms in threads - how to get/mimic?

Steve Holden sholden at
Mon Dec 18 09:51:55 EST 2000

Lloyd Zusman <ljz at> wrote in message
news:ltd7erk8xd.fsf at
> Luca de Alfaro <dealfaro at> writes:
> > [ ... ]
> >
> > What is not clear to me is the following.  When there is a timeout,
> > the exception Timeout is not caught by httplib.  Hence, if the thread
> > that called httplib just continues its job giving up on the
> > connection, httplib will not have a chance to do any clean-up, such as
> > for example releasing/closing the socket.
> >
> > Hence, it seems to me that in order to abort a web page load cleanly
> > upon timeout, I also have to modify the httplib module to handle the
> > exception Timeout, and do clean-up work if that happens.
> >
> > Is my understanding correct?
> Not entirely.  When using timeoutsocket, all you have to do is start
> catching timeout exceptions when you make the various method calls
> within httplib
[snip code]

Basically, if the code you make use of traps the errors raised by
timeoutsocket and re-raises them after it's done whatever cleanup it needs
to then there should be no problem.  However, there MAY be problems if the
timeoutsocket error is not caught by the higher-level protocol code, and
makes it to the outer code which called it.  In that case there is a
possibility that necessary clean-up may not be performed.
> It's recommended in Python that exception handling be done whenever
> it's appropriate; therefore, by wrapping your httplib calls in this
> same manner, you'll remain true to the Pythonic Way, and you won't
> have to re-write any httplib code when using timeoutsocket.
> Note that the timeoutsocket.setDefaultSocketTimeout() method call
> should be made before making any httplib calls.  If you want to use
> different timeout values when making different httplib calls (or other
> calls socket-based methods), you'll have to re-invoke the
> setDefaultSocketTimeout() method before each call that uses a changed
> timeout value.
Note also that you can set the timeout on each socket individually.
setDefaultSocketTimeout is usually used to ensure that connect() attempts in
upper-layer protocols (which happen before you have a socket object in your
application to manipulate) don't hang forever.


More information about the Python-list mailing list