How to run a Magma computation with a timeout

31 views
Skip to first unread message

Tzanko Matev

unread,
Mar 28, 2011, 11:15:41 AM3/28/11
to sage-support
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

William Stein

unread,
Mar 28, 2011, 2:28:36 PM3/28/11
to sage-s...@googlegroups.com, 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).

Tzanko Matev

unread,
Mar 30, 2011, 5:15:38 AM3/30/11
to sage-support
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.
Reply all
Reply to author
Forward
0 new messages