views:

44

answers:

1

I have some method doA() that occasionally hangs for a while. Are there any common modules in Python that can control time of doA() execution and interrupt it? Of course, it may be implemented via threads, so simple wrapper around threading module may be good solution.

In other words i'd like to have code like:

import CoolAsyncControl
CoolAsyncControl.call(self.doA, timeout = 10)
+5  A: 

You can use a threading.Timer to call thread.interrupt_main, as long as you're running doA in the main thread. Note that the syntax you desire is impossible because Python (like most languages, excepting e.g. Haskell) is "eager" -- arguments are entirely computed before a call is performed, so the self.doA() would run to completion before the call method has any chance to do anything about it! You'll have to use some syntax such as

CoolIt.call(self.doA, args=(), timeout=100)

so that call can set the timer up before performing the call to doA with the given arguments.

Alex Martelli
Thanks! It is just what i'm looking for. Of course, i mean self.doA in my code
shuvalov