| 1 | initial version |
In py previous post, i guessed that the problem comes from cython, that does not handle the Python signal correctly, as if it runs too fast to hear it ;) Here is a workaround: the timetout() function should operate at a lower level. We will launch the computation as a new thread, and kill the thread after timeout_duration. The kill signal is not sent at the Python level, but at the kernel level.
To compute a function in a separate thread, Sage has a @fork decorator, here is how to use it in your framework:
sage: def timeout(func, args=(), kwargs={}, timeout_duration=10):
....: @fork(timeout=timeout_duration, verbose=True)
....: def my_new_func():
....: return func(*args, **kwargs)
....: my_new_func()
Now you can see that oth
sage: timeout(loop_forever)
and
sage: timeout(my_func)
stop after time_duration !
In py previous post, i guessed that the problem comes from cython, that does not handle the Python signal correctly, as if it runs too fast to hear it ;) Here is a workaround: the timetout() function should operate at a lower level. We will launch the computation as a new thread, and kill the thread after timeout_duration. The kill signal is not sent at the Python level, but at the kernel level.
To compute a function in a separate thread, Sage has a @fork decorator, here is how to use it in your framework:
sage: def timeout(func, args=(), kwargs={}, timeout_duration=10):
....: @fork(timeout=timeout_duration, verbose=True)
....: def my_new_func():
....: return func(*args, **kwargs)
....: my_new_func()
Now you can see that othboth
sage: timeout(loop_forever)
and
sage: timeout(my_func)
stop after time_duration !
In py my previous post, i guessed that the problem comes from cython, that does not handle the Python signal correctly, as if it runs too fast to hear it ;) Here is a workaround: the timetout() function should operate at a lower level. We will launch the computation as a new thread, and kill the thread after timeout_duration. The kill signal is not sent at the Python level, but at the kernel level.
To compute a function in a separate thread, Sage has a @fork decorator, here is how to use it in your framework:
sage: def timeout(func, args=(), kwargs={}, timeout_duration=10):
....: @fork(timeout=timeout_duration, verbose=True)
....: def my_new_func():
....: return func(*args, **kwargs)
....: return my_new_func()
Now you can see that both
sage: timeout(loop_forever)
and
sage: timeout(my_func)
stop after time_duration !
| 4 | No.4 Revision |
In my previous post, answer, i guessed that the problem comes from cython, that does not handle the Python signal correctly, as if it runs too fast to hear it ;) Here is a workaround: the timetout() function should operate at a lower level. We will launch the computation as a new thread, and kill the thread after timeout_duration. The kill signal is not sent at the Python level, but at the kernel level.
To compute a function in a separate thread, Sage has a @fork decorator, here is how to use it in your framework:
sage: def timeout(func, args=(), kwargs={}, timeout_duration=10):
....: @fork(timeout=timeout_duration, verbose=True)
....: def my_new_func():
....: return func(*args, **kwargs)
....: return my_new_func()
Now you can see that both
sage: timeout(loop_forever)
and
sage: timeout(my_func)
stop after time_duration !
| 5 | thread -> process |
In my previous answer, i guessed that the problem comes from cython, that does not handle the Python signal correctly, as if it runs too fast to hear it ;) Here is a workaround: the timetout() function should operate at a lower level. We will launch the computation as a new thread, process, and kill the thread process after timeout_duration. The kill signal is not sent at the Python level, but at the kernel level.
To compute a function in a separate thread, process, Sage has a @fork decorator, here is how to use it in your framework:
sage: def timeout(func, args=(), kwargs={}, timeout_duration=10):
....: @fork(timeout=timeout_duration, verbose=True)
....: def my_new_func():
....: return func(*args, **kwargs)
....: return my_new_func()
Now you can see that both
sage: timeout(loop_forever)
and
sage: timeout(my_func)
stop after time_duration !
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.