# Revision history [back]

The first reason why this won't work is that factor(Primes().next(2^400)*Primes().next(2^500)) is an object of the class sage.structure.factorization_integer.IntegerFactorization, but not a function. Hence if you send it to the timeout() function, it will be computed before being sent to the timeout() function, which won't kill the computation.

Hence, you will experience the same problem as if you did:

sage: timeout(loop_forever())


Which will loop infinitely. So, you should do something like :

sage: def my_func():
....:     return factor(Primes().next(2^400) * Primes().next(2^500))
sage: timeout(my_func)


And, surprise, it doesn't work better !

Here is a possible reason: as you can check by typing;

sage: a = 123
sage: a.factor??


the factor() function is made of cython (see the use of cdef). So this may be the problem, let's inspect this. For that, open a notebook and create one cell with:

%cython
def loop_forever():
import time
while True:
time.sleep(1)
return 1;


(this will create the loop_forever() function as a cython one), and another one with:

import signal

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)


Then try:

timeout(loop_forever)


As you can see, the computation does not stop either ! I guess cython is the reason why your problem happened.

The first reason why this won't does not work is that the expression factor(Primes().next(2^400)*Primes().next(2^500)) is an object of the class sage.structure.factorization_integer.IntegerFactorization, but not a function. Hence Hence, if you send it this expression to the timeout() function, it will be computed before being sent to the timeout() function, which won't kill the computation.

computation. Hence, you will experience the same problem as if you did:

sage: timeout(loop_forever())


Which will loop infinitely. So, you should do something like : like:

sage: def my_func():
....:     return factor(Primes().next(2^400) * Primes().next(2^500))
sage: timeout(my_func)


And, surprise, it doesn't work better !

Here is a possible reason: as reason. As you can check by typing;

sage: a = 123
sage: a.factor??


the factor() function is made of cython (see the use of cdef). So this may be the problem, let's inspect this. investigate this track further. For that, open a notebook and create one cell with:

%cython
def loop_forever():
import time
while True:
time.sleep(1)
return 1;


(this will create the loop_forever() function as a cython one), and another one cell with:

import signal

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)


Then try:

timeout(loop_forever)


As you can see, the computation does not stop either ! I guess cython is the reason why your problem happened.

The first reason why this does not work is that the expression factor(Primes().next(2^400)*Primes().next(2^500)) is an object of the class sage.structure.factorization_integer.IntegerFactorization, but not a function. Hence, if you send this expression to the timeout() function, it will be computed before being sent to the timeout() function, which won't kill the computation. Hence, you will experience the same problem as if you did:

sage: timeout(loop_forever())


Which will loop infinitely. So, you should do something like:

sage: def my_func():
....:     return factor(Primes().next(2^400) * Primes().next(2^500))
sage: timeout(my_func)


And, surprise, it doesn't work better ! Here is a possible reason. As you can check by typing;typing:

sage: a = 123
sage: a.factor??


the factor() function is made of cython (see the use of cdef). So this may be the problem, let's investigate this track further. For that, open a notebook and create one cell with:

%cython
def loop_forever():
import time
while True:
time.sleep(1)
return 1;


(this will create the loop_forever() function as a cython one), and another cell with:

import signal

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)


Then try:

timeout(loop_forever)


As you can see, the computation does not stop either ! ! I guess cython is the reason why your problem happened.