Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cross-platform time out decorator

1 view
Skip to first unread message

Joel

unread,
Sep 26, 2007, 6:21:55 AM9/26/07
to
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

Tim Golden

unread,
Sep 27, 2007, 7:57:34 AM9/27/07
to Joel, pytho...@python.org

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

kyos...@gmail.com

unread,
Sep 27, 2007, 9:27:54 AM9/27/07
to
On Sep 26, 5:21 am, Joel <joel.schae...@gmail.com> wrote:
> 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

Joel

unread,
Sep 27, 2007, 9:31:22 AM9/27/07
to
On Sep 26, 12:21 pm, Joel <joel.schae...@gmail.com> wrote:
> 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

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.

Hrvoje Niksic

unread,
Sep 27, 2007, 10:36:32 AM9/27/07
to
Joel <joel.s...@gmail.com> writes:

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

Joel

unread,
Sep 27, 2007, 11:39:13 AM9/27/07
to
On Sep 27, 4:36 pm, Hrvoje Niksic <hnik...@xemacs.org> wrote:

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?

Steve Holden

unread,
Sep 27, 2007, 11:54:30 AM9/27/07
to pytho...@python.org
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

Tim Golden

unread,
Sep 27, 2007, 12:06:04 PM9/27/07
to pytho...@python.org
Steve Holden wrote:
> 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

Hrvoje Niksic

unread,
Sep 27, 2007, 3:55:37 PM9/27/07
to
Joel <joel.s...@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.

Chris Mellon

unread,
Sep 27, 2007, 4:40:16 PM9/27/07
to pytho...@python.org
> --


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.

Hrvoje Niksic

unread,
Sep 27, 2007, 5:38:49 PM9/27/07
to
"Chris Mellon" <ark...@gmail.com> writes:

> You can use ctypes and the Python API to raise a Python exception in
> the thread.

How, by changing the thread's exception state?

Joel

unread,
Oct 1, 2007, 3:34:38 AM10/1/07
to
On Sep 27, 10:40 pm, "Chris Mellon" <arka...@gmail.com> wrote:
> On 9/27/07, Hrvoje Niksic <hnik...@xemacs.org> wrote:

>
>
>
> > Joel <joel.schae...@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.
> > --
>
> 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?

Gabriel Genellina

unread,
Oct 1, 2007, 7:14:36 PM10/1/07
to pytho...@python.org
En Mon, 01 Oct 2007 04:34:38 -0300, Joel <joel.s...@gmail.com>
escribi�:

> On Sep 27, 10:40 pm, "Chris Mellon" <arka...@gmail.com> wrote:

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

0 new messages