Cross-platform time out decorator

Hrvoje Niksic hniksic at xemacs.org
Thu Sep 27 15:55:37 EDT 2007


Joel <joel.schaerer at gmail.com> writes:

>> Note that, unlike the original alarm code, it doesn't really interrupt
>> the timed-out method, it just returns the control back to the caller,
>> using an exception to mark that a timeout occurred.  The "timed out"
>> code is still merrily running in the background.  I don't know if it's
>> a problem in your case, but it's an important drawback.
>
> There should be a method to stop the thread though?

Not in Python.  Thread killing primitives differ between systems and
are unsafe in general, so they're not exposed to the interpreter.  On
Windows you can attempt to use ctypes to get to TerminateThread, but
you'll need to hack at an uncomfortably low level and be prepared to
deal with the consequences, such as memory leaks.  If the timeouts
happen rarely and the code isn't under your control (so you have no
recourse but to terminate the thread), it might be worth it though.



More information about the Python-list mailing list