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.Wed, 22 Apr 2020 13:09:34 +0200@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/