Cross-platform time out decorator

T

Tim Golden

Joel said:
I've been using this nice timing out decorator :
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307871 . The
problem is that since it relies on sigalarm, it doesn't work under
windows. Would anyone know how to do a cross-platform version?

I don't think you're going to find anything straightforward. AFAIK,
there's nothing on the Windows side of things which is directly
equivalent to the signals business in *nix. Sure, you can mess around
with timers and threads and events and so on. But it's far from being
the built-in tool which the signal module gives you.

TJG
 
K

kyosohma

I've been using this nice timing out decorator :http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307871. The
problem is that since it relies on sigalarm, it doesn't work under
windows. Would anyone know how to do a cross-platform version?

Thanks a lot!

joel

You might be able to use the timeit module.

http://docs.python.org/lib/module-timeit.html

Some people like to use hotshot:

http://www.onlamp.com/pub/a/python/2005/12/15/profiling.html

I doubt this is what you're looking for, but maybe it'll give you a
push in the right direction.

Mike
 
H

Hrvoje Niksic

Joel said:
I found the solution :
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440569
describes a solution based on threads. I tested it and it works
perfectly.

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.
 
J

Joel

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? I've never
programmed thread stuff in python and wasn't able to find how to do
it, would you happen to know how to "kill" the timed out thread?
 
S

Steve Holden

Joel said:
There should be a method to stop the thread though? I've never
programmed thread stuff in python and wasn't able to find how to do
it, would you happen to know how to "kill" the timed out thread?
There is no way to "kill" a thread, other than set a flag and have the
thread read it to realise the main thread wants it to stop.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
T

Tim Golden

Steve said:
There is no way to "kill" a thread, other than set a flag and have the
thread read it to realise the main thread wants it to stop.

This is more-or-less why I suggested earlier that you wouldn't
find anything straightforward. The signal mechanism, as far as
I know, is pretty much unique in terms of the support it gets
from the OS and the language combined. Any other solution will
end up papering over cracks.

TJG
 
H

Hrvoje Niksic

Joel said:
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.
 
C

Chris Mellon

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.
--


You can use ctypes and the Python API to raise a Python exception in
the thread. I don't normally mention this, because it has some
limitations, but it results in essentially the same effect as the
signal based method. They both have the limitation that C code can't
be interrupted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top