This is part of a complex project, I will try and simplify it.
I have a class that gets a callable and executes it, the callable can run for any duration of time. If I get a signal (can be using Signal or any other flag I watch) to terminate I want to terminate the callable's execution on the spot (without exiting the process of course)
class FooRunner(object):
def goo(self, foo):
try:
foo()
except:
pass
def on_stop_signal(self):
pass
If anyone ever needs this here is a code sample of it working (One thing to note
signal.signal
can be called only from the main thread):Thanks @jsbueno
On a single-threaded signal not running on Windows, (i.e., any Unix flavor) you can use signal.alarm for that.
Check the first example on the documentation - it is more or less what you are asking for: https://docs.python.org/2/library/signal.html