Catch script hangs

exarkun at exarkun at
Mon Sep 28 01:28:08 CEST 2009

On 10:40 pm, bakes at wrote:
>Due to an ftp server issue, my python script sometimes hangs whilst
>downloading, unable to receive any more data. Is there any way that I
>could have python check, maybe through a thread or something, whether
>it has hanged (or just, if it's still active after 10 seconds, stop
>it?). I have looked at threading but there does not seem to be a stop
>method on threading, which is a problem. Could the lower level thread
>module be a solution?

No.  There are a great many issues which arise when trying to forcibly 
terminate a thread.  Python doesn't expose this functionality because 
most platforms don't provide it in a safe or reliable way.

You could give Twisted's FTP client a try.  Since it isn't blocking, you 
don't need to use threads, so it's easy to have a timeout.

You could also explore solutions based on signal.alarm().  A single- 
threaded signal-based solution has some issues as well, but not nearly 
as many as a thread-based solution.


More information about the Python-list mailing list