471,357 Members | 1,099 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

Cross-platform time out decorator

I've been using this nice timing out decorator :
http://aspn.activestate.com/ASPN/Coo.../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

Sep 26 '07 #1
12 2966
Joel wrote:
I've been using this nice timing out decorator :
http://aspn.activestate.com/ASPN/Coo.../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
Sep 27 '07 #2
On Sep 26, 5:21 am, Joel <joel.schae...@gmail.comwrote:
I've been using this nice timing out decorator :http://aspn.activestate.com/ASPN/Coo.../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/2...profiling.html

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

Mike

Sep 27 '07 #3
On Sep 26, 12:21 pm, Joel <joel.schae...@gmail.comwrote:
I've been using this nice timing out decorator :http://aspn.activestate.com/ASPN/Coo.../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
I found the solution : http://aspn.activestate.com/ASPN/Coo.../Recipe/440569
describes a solution based on threads. I tested it and it works
perfectly.

Sep 27 '07 #4
Joel <jo***********@gmail.comwrites:
I found the solution :
http://aspn.activestate.com/ASPN/Coo.../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.
Sep 27 '07 #5
On Sep 27, 4:36 pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
Joel <joel.schae...@gmail.comwrites:
I found the solution :
http://aspn.activestate.com/ASPN/Coo.../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.
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?

Sep 27 '07 #6
Joel wrote:
On Sep 27, 4:36 pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
>Joel <joel.schae...@gmail.comwrites:
>>I found the solution :
http://aspn.activestate.com/ASPN/Coo.../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.

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

Sep 27 '07 #7
Steve Holden wrote:
Joel wrote:
>On Sep 27, 4:36 pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
>>Joel <joel.schae...@gmail.comwrites:
I found the solution :
http://aspn.activestate.com/ASPN/Coo.../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.
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.
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
Sep 27 '07 #8
Joel <jo***********@gmail.comwrites:
>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.
Sep 27 '07 #9
On 9/27/07, Hrvoje Niksic <hn*****@xemacs.orgwrote:
Joel <jo***********@gmail.comwrites:
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.
--

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.
Sep 27 '07 #10
"Chris Mellon" <ar*****@gmail.comwrites:
You can use ctypes and the Python API to raise a Python exception in
the thread.
How, by changing the thread's exception state?
Sep 27 '07 #11
On Sep 27, 10:40 pm, "Chris Mellon" <arka...@gmail.comwrote:
On 9/27/07, Hrvoje Niksic <hnik...@xemacs.orgwrote:
Joel <joel.schae...@gmail.comwrites:
>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.
--

You can use ctypes and the Python API to raise a Python exception in
the thread.
Could you point me to an example of how to do that?
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.

Oct 1 '07 #12
En Mon, 01 Oct 2007 04:34:38 -0300, Joel <jo***********@gmail.com>
escribi�:
On Sep 27, 10:40 pm, "Chris Mellon" <arka...@gmail.comwrote:
>You can use ctypes and the Python API to raise a Python exception in
the thread.
Could you point me to an example of how to do that?
Found this: <http://sebulba.wikispaces.com/recipe+thread2>
Looks promising but I've never tried it actually.

--
Gabriel Genellina

Oct 1 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ampo | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.