473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3108
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
6
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.