ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Jul 2021 15:10:22 +0200Catch exception from forked subprocesshttps://ask.sagemath.org/question/57886/catch-exception-from-forked-subprocess/ I use `fork` to give certain methods only a fixed time for execution. These methods might, however, raise an exception and I don't know how to catch these. The code:
@fork
def test() :
raise ValueError("Found a value error")
try :
test()
except ValueError :
print("Value error caught")
does not catch the error. Is there a way to do this?philipp7Tue, 06 Jul 2021 15:10:22 +0200https://ask.sagemath.org/question/57886/@fork decorator with try/except on different platforms, not executing except clausehttps://ask.sagemath.org/question/50912/fork-decorator-with-tryexcept-on-different-platforms-not-executing-except-clause/ I am having problems using the **@fork** decorator with a try & except clause. On SageMathCell the piece of code runs fine, whereas on both Jupyter and CoCalc it doesn't throw the exception clause properly. On CoCalc it didn't recognise the **@fork** decorator at all at first, but `from sage.all import *` (idea from question: "@fork decorator not recognized in script") seemed to help.
**Piece of code:**
from sage.all import * #for CoCalc
a_0,a_1 = var('a_0,a_1');s = [a_0,a_1]
equations = [69*a_0 + 4556 == 69*a_0 + 63*a_1, 69*a_1 - 3350 == -67*a_0 + 57*a_1, 63*a_0 - 3876 == -1542, 63*a_1 + 2850 == 7406]
try:
@fork(timeout=0.1, verbose=True) #use e.g. 0.1 and 10
def DirectSolution():
sage_solution = solve(equations, s , solution_dict=True)
print('Solves in time ,','sage_solution:',sage_solution)
return sage_solution
sage_solution = DirectSolution()[0]
except KeyboardInterrupt:
sage_solution = []
print('Takes too long , ','sage_solution:',sage_solution)
print('Execute the rest of the code, ','sage_solution:',sage_solution)
Running the @fork decorator without try/except with 0.1 seconds I got the `KeyboardInterrupt` error, that's why I used it in the except clause. Shouldn't this exception usually be "raised when the user hits the interrupt key" ?
- As mentioned above, on SageMathCell
the code works as intended:
With 10 seconds it computes the
solution and prints out the text. For
0.1 seconds it sets
`sage_solution = []` and prints out
the text.
- However, both on Jupyter and CoCalc it doesn't use the exception properly for 0.1 seconds.
I get the following message:
`Killing subprocess 1346 with input ((), {}) which took too long`
`Execute the rest of the code, sage_solution: N`
Meaning it didn't execute the except clause:
`sage_solution = []`
`print('Takes too long , ','sage_solution:',sage_solution)`
I am not sure what to look for here, because it is working on one platform. A simple try/except example worked fine. Another idea was that the `KeyboardInterrupt` could be the problem, but removing it didn't change anything.
I am new to Sage/Python, so there probably is a simple solution but I am happy for any help given. Unfortunately my karma was insufficient to publish links.RikWed, 22 Apr 2020 13:09:34 +0200https://ask.sagemath.org/question/50912/@fork decorator not recognized in scripthttps://ask.sagemath.org/question/10628/fork-decorator-not-recognized-in-script/I'm trying to run a test script with the @fork decorator, but it isn't being recognized. The script looks like this:
#!/home/raltman/sage-5.10/sage
@fork
def f(a):
return a;
out=f("hi");
print out;
When I run this, I get the following error message:
> Traceback (most recent call last):
> File "/home/raltman/test.sage", line
> 3, in <module>
>
>@fork
>
>NameError: name 'fork' is not defined
The same code works when I run it in the sage console, and the script itself works without the @fork decorator. I'd really appreciate any help on this!R AltmanSat, 19 Oct 2013 06:42:38 +0200https://ask.sagemath.org/question/10628/