Thanks a lot!
joel
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
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
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.
> 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.
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?
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
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
>> 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.
> You can use ctypes and the Python API to raise a Python exception in
> the thread.
How, by changing the thread's exception state?
Could you point me to an example of how to do that?
> 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