Kill the thread in a long computation
Hello
I'm trying to run a long factor() computation and kill it if it took more than 10 seconds. As a simple example to kill a thread in python using signals consider the following code:
def loop_forever():
import time
while True:
time.sleep(1)
return 1;
def handler(signum, frame):
raise Exception("end of time")
def timeout(func, args=(), kwargs={}, timeout_duration=10):
signal.signal(signal.SIGALRM, handler)
signal.alarm(timeout_duration)
try:
func(*args, **kwargs)
except Exception:
pass
signal.alarm(0)
timeout(loop_forever)
This would run fine and the thread would be killed after 10 seconds
If on the other hand instead of calling loop_forever() I call
def crackKey():
return factor(Primes().next(2^400) * Primes().next(2^500))
timeout(crackKey)
The thread is not killed after 10 seconds and it continues the computation Any idea why this is happening?
cheers