The alarm() function was exactly what I needed. Thanks! I had tried to attach my own handler to SIGALRM, but because of, I guess, the way Sage handles exceptions it dind't work.
On Mar 28, 8:28 pm, William Stein <[email protected]> wrote: > On Mon, Mar 28, 2011 at 8:15 AM, Tzanko Matev <[email protected]> wrote: > > Hi, > > > I want to run a certain computation in Magma from a Sage script, > > however I would like to interrupt the computation if it takes longer > > than a set amount of time. Is it possible to do that? > > Thanks in advance, > > Tzanko Matev > > Depending on the level of nastiness of the Magma code you're using, > something based on this may work for you. > > def factor_or_die_trying(n, timeout=5): > try: > alarm(timeout) > return magma('Factorization(%s)'%n) > except: > return "timed out" > finally: > alarm(0) > > sage: factor_or_die_trying(2^15-1, 3) > [ <7, 1>, <31, 1>, <151, 1> ] > sage: factor_or_die_trying(2^997-1, 3) > Interrupting Magma... > 'timed out' > > Alternatively, Magma itself also has an Alarm command: > > (in Magma) > > > Alarm; > > Intrinsic 'Alarm' > > Signatures: > > (<RngIntElt> n) > > Send alarm signal after n seconds to Magma (UNIX only). -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org
