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, 13 Jul 2011 22:51:34 +0200How can I add a timeout to a charpoly calculation?https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/For some reason after a bunch of matrix charpoly calculations, charpoly hangs on me. I suppose it's some memory problem, but rather that fix that, I just want to put a timeout on the calculation and then restart it if it hangs.
The problem is I can't use the "alarm" solution given in the answer to [this question](http://ask.sagemath.org/question/369/any-way-to-timeout)
because sage.matrix.matrix_integer_dense.Matrix_integer_dense.charpoly doesn't catch the KeyboardInterrupt:
sage: M = random_matrix(ZZ,1000,1000)
sage: alarm(30)
sage: try:
....: P = M.charpoly()
....: except KeyboardInterrupt:
....: print "did not complete!"
....:
What is another approach I could use?Tue, 12 Jul 2011 00:06:42 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/Answer by parzan for <p>For some reason after a bunch of matrix charpoly calculations, charpoly hangs on me. I suppose it's some memory problem, but rather that fix that, I just want to put a timeout on the calculation and then restart it if it hangs.</p>
<p>The problem is I can't use the "alarm" solution given in the answer to <a href="http://ask.sagemath.org/question/369/any-way-to-timeout">this question</a>
because sage.matrix.matrix_integer_dense.Matrix_integer_dense.charpoly doesn't catch the KeyboardInterrupt:</p>
<pre><code>sage: M = random_matrix(ZZ,1000,1000)
sage: alarm(30)
sage: try:
....: P = M.charpoly()
....: except KeyboardInterrupt:
....: print "did not complete!"
....:
</code></pre>
<p>What is another approach I could use?</p>
https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?answer=12509#post-id-12509Here are answers from various python sources:
[http://nick.vargish.org/clues/python-tricks.html](http://nick.vargish.org/clues/python-tricks.html)
[http://pguides.net/python/timeout-a-function](http://pguides.net/python/timeout-a-function)
[http://stackoverflow.com/questions/492519/timeout-on-a-python-function-call](http://stackoverflow.com/questions/492519/timeout-on-a-python-function-call)
Tue, 12 Jul 2011 06:00:43 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?answer=12509#post-id-12509Comment by Max Flander for <p>Here are answers from various python sources:</p>
<p><a href="http://nick.vargish.org/clues/python-tricks.html">http://nick.vargish.org/clues/python-tricks.html</a></p>
<p><a href="http://pguides.net/python/timeout-a-function">http://pguides.net/python/timeout-a-function</a></p>
<p><a href="http://stackoverflow.com/questions/492519/timeout-on-a-python-function-call">http://stackoverflow.com/questions/492519/timeout-on-a-python-function-call</a></p>
https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21483#post-id-21483thanks for the suggestions, i can't get any of them to work though... for some reason charpoly ignores KeyboardException, SystemExit and so on, in fact sometimes when you do ctrl-c to stop it, you get a seg-fault... so maybe there's no easy answer?Tue, 12 Jul 2011 12:09:14 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21483#post-id-21483Answer by Max Flander for <p>For some reason after a bunch of matrix charpoly calculations, charpoly hangs on me. I suppose it's some memory problem, but rather that fix that, I just want to put a timeout on the calculation and then restart it if it hangs.</p>
<p>The problem is I can't use the "alarm" solution given in the answer to <a href="http://ask.sagemath.org/question/369/any-way-to-timeout">this question</a>
because sage.matrix.matrix_integer_dense.Matrix_integer_dense.charpoly doesn't catch the KeyboardInterrupt:</p>
<pre><code>sage: M = random_matrix(ZZ,1000,1000)
sage: alarm(30)
sage: try:
....: P = M.charpoly()
....: except KeyboardInterrupt:
....: print "did not complete!"
....:
</code></pre>
<p>What is another approach I could use?</p>
https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?answer=12420#post-id-12420OK here's my horrible workaround using rob hooft's [task.py](http://python.net/crew/hooft/):
Write my matrix to a text-file. Put the charpoly calculation in a separate file called <tt>charpoly.sage</tt> which reads in the matrix from that text-file, and outputs its result to another text-file. Then in the main code run the following:
from os import system
load task.py
maxtime = 3600
success = 0
while success == 0:
task = Task("sage charpoly.sage")
task.Run()
time = 0
while time <= maxtime:
sleep(1)
time += 1
if task.Done() == 1:
success = 1
break
if time > maxtime:
task.Kill()
system("PIDS=$(ps -ef| grep -e 'python charpoly.py' | grep -v grep |awk '{print $2}')")
system("kill -9 $PIDS")
print ("timeout!")
That last rubbish is because sage spawns some other python processes which eat up all the cpu and are not killed by task.Kill(). It's not pretty but it'll do the trick for now. The reason that I simply restart the calculation after a timeout is that it only sporadically gets stuck, and restarting seems to get it going again, go figure...
Wed, 13 Jul 2011 04:50:08 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?answer=12420#post-id-12420Comment by Max Flander for <p>OK here's my horrible workaround using rob hooft's <a href="http://python.net/crew/hooft/">task.py</a>:</p>
<p>Write my matrix to a text-file. Put the charpoly calculation in a separate file called <tt>charpoly.sage</tt> which reads in the matrix from that text-file, and outputs its result to another text-file. Then in the main code run the following:</p>
<pre><code> from os import system
load task.py
maxtime = 3600
success = 0
while success == 0:
task = Task("sage charpoly.sage")
task.Run()
time = 0
while time <= maxtime:
sleep(1)
time += 1
if task.Done() == 1:
success = 1
break
if time > maxtime:
task.Kill()
system("PIDS=$(ps -ef| grep -e 'python charpoly.py' | grep -v grep |awk '{print $2}')")
system("kill -9 $PIDS")
print ("timeout!")
</code></pre>
<p>That last rubbish is because sage spawns some other python processes which eat up all the cpu and are not killed by task.Kill(). It's not pretty but it'll do the trick for now. The reason that I simply restart the calculation after a timeout is that it only sporadically gets stuck, and restarting seems to get it going again, go figure...</p>
https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21474#post-id-21474that's true :)Wed, 13 Jul 2011 22:51:34 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21474#post-id-21474Comment by parzan for <p>OK here's my horrible workaround using rob hooft's <a href="http://python.net/crew/hooft/">task.py</a>:</p>
<p>Write my matrix to a text-file. Put the charpoly calculation in a separate file called <tt>charpoly.sage</tt> which reads in the matrix from that text-file, and outputs its result to another text-file. Then in the main code run the following:</p>
<pre><code> from os import system
load task.py
maxtime = 3600
success = 0
while success == 0:
task = Task("sage charpoly.sage")
task.Run()
time = 0
while time <= maxtime:
sleep(1)
time += 1
if task.Done() == 1:
success = 1
break
if time > maxtime:
task.Kill()
system("PIDS=$(ps -ef| grep -e 'python charpoly.py' | grep -v grep |awk '{print $2}')")
system("kill -9 $PIDS")
print ("timeout!")
</code></pre>
<p>That last rubbish is because sage spawns some other python processes which eat up all the cpu and are not killed by task.Kill(). It's not pretty but it'll do the trick for now. The reason that I simply restart the calculation after a timeout is that it only sporadically gets stuck, and restarting seems to get it going again, go figure...</p>
https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21481#post-id-21481Horrible is a matter of taste - at least you got it working!Wed, 13 Jul 2011 13:44:25 +0200https://ask.sagemath.org/question/8219/how-can-i-add-a-timeout-to-a-charpoly-calculation/?comment=21481#post-id-21481