Alarms in threads - how to get/mimic?

Luca de Alfaro dealfaro at eecs.berkeley.edu
Sat Dec 16 06:05:00 EST 2000


I am trying to write a multi-threaded program to read web pages.  The
program spawns (say) 10 threads, assigning to each thread a file to
read with the ftp or http protocol; each thread reads it, and returns
the info.

The problem is that the threads sometimes hang for a very long time
waiting for a connection (httplib seems to have very long timeouts??),
whereas instead I don't care for the answer if it comes later than
(say) a few seconds.

The obvious solution would be to have each thread set an alarm before
trying the url read operation.  However, alarms cannot be used in
threads in Python (they can only be used in the main thread). 

The next solution would be to set an alarm in the main thread, and if
the alarm goes off while waiting for some subthreads to be join-ed,
kill the laggard threads that can't be joined.  This is fine, except
that (a) I cannot kill subthreads in Python, and (b) I don't want to
risk killing a thread at an arbitrary point (it can cause
inconsistencies), but only while it's waiting for a connection.

Have I reached the limits of Python?  Should I be coding in Java
instead?  Please, tell me it's not true, and that there's an obvious
solution...  I like Python much better than Java otherwise...

Thanks, 

Luca de Alfaro





More information about the Python-list mailing list