Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to stop code after a given time

My question is the following: sometimes a computation runs too long, and I would rather for it to stop automatically, and move on to the next computation (in the best possible world keep a list of the cases that were skipped).

So I wanted to implement the solution to this (essentially) identical question:

https://stackoverflow.com/questions/14920384/stop-code-after-time-period

However in my case the function "foo" uses some functionalities of GAP. This apparently causes a crash:

** Gap crashed or quit executing '__SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;' **
Restarting Gap and trying again

The crash happens immediately on the first call of the GAP function.

Is there any way to avoid this?

here is the rest of the error:

Process Foo:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ptyprocess/ptyprocess.py", line 711, in isalive
    pid, status = os.waitpid(self.pid, waitpid_options)
ChildProcessError: [Errno 10] No child processes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 23, in _wrap_ptyprocess_err
    yield
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 705, in isalive
    alive = ptyproc.isalive()
  File "/usr/lib/python3/dist-packages/ptyprocess/ptyprocess.py", line 715, in isalive
    raise PtyProcessError('isalive() encountered condition ' +
ptyprocess.util.PtyProcessError: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 658, in _eval_line
    (normal, error) = self._execute_line(line, wait_for_prompt=wait_for_prompt,
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 524, in _execute_line
    x = E.expect_list(self._compiled_full_pattern)
  File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 169, in expect_loop
    incoming = spawn.read_nonblocking(spawn.maxread, timeout)
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 478, in read_nonblocking
    if not self.isalive():
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 704, in isalive
    with _wrap_ptyprocess_err():
  File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 25, in _wrap_ptyprocess_err
    raise ExceptionPexpect(*e.args)
pexpect.exceptions.ExceptionPexpect: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 673, in _eval_line
    raise RuntimeError("%s produced error output\n%s\n   executing %s"%(self, error,line))
RuntimeError: Gap produced error output
Error, <size> must be a positive integer

   executing __SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/tmp/ipykernel_115920/3461510396.py", line 85, in dobblegppack
    A = gap(nn).AllSmallGroups()
  File "/usr/lib/python3/dist-packages/sage/interfaces/interface.py", line 693, in __call__
    return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 856, in function_call
    res = self.eval(marker+cmd)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 498, in eval
    result = Expect.eval(self, input_line, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/expect.py", line 1382, in eval
    return '\n'.join([self._eval_line(L, allow_use_file=allow_use_file, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/expect.py", line 1382, in <listcomp>
    return '\n'.join([self._eval_line(L, allow_use_file=allow_use_file, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 703, in _eval_line
    return self._eval_line(line, allow_use_file=allow_use_file)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 707, in _eval_line
    raise RuntimeError(exc)
RuntimeError: Gap produced error output
Error, <size> must be a positive integer

   executing __SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;

How to stop code after a given time

My question is the following: sometimes a computation runs too long, and I would rather for it to stop automatically, and move on to the next computation (in the best possible world keep a list of the cases that were skipped).

So I wanted to implement the solution to this (essentially) identical question:

https://stackoverflow.com/questions/14920384/stop-code-after-time-period

However in my case the function "foo" uses some functionalities of GAP. This apparently causes a crash:

** Gap crashed or quit executing '__SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;' **
Restarting Gap and trying again

The crash happens immediately on the first call of the GAP function.

function. Is there any way to avoid this?

here EDIT

The suggestion with alarm did not work for me (maybe it has to with my implementation). Basically the first interruption works correctly (after 20 seconds), then follows five minutes where nothing happens, after these five minutes the second iteration of the for loop begins, this second iteration is the rest of the error:

Process Foo:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/ptyprocess/ptyprocess.py", from cysignals.alarm import alarm, AlarmInterrupt, cancel_alarm

for i in range(0,10):
    try:
        alarm(20)    
        print("Began computation at", time.localtime())
        foo(i)
    except AlarmInterrupt:      
        print("Timed out with ", i, "at", time.localtime())
        cancel_alarm()  ## the presence or absence of this line 711, in isalive
    pid, status = os.waitpid(self.pid, waitpid_options)
ChildProcessError: [Errno 10] No child processes

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 23, in _wrap_ptyprocess_err
    yield
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 705, in isalive
    alive = ptyproc.isalive()
  File "/usr/lib/python3/dist-packages/ptyprocess/ptyprocess.py", line 715, in isalive
    raise PtyProcessError('isalive() encountered condition ' +
ptyprocess.util.PtyProcessError: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 658, in _eval_line
    (normal, error) = self._execute_line(line, wait_for_prompt=wait_for_prompt,
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 524, in _execute_line
    x = E.expect_list(self._compiled_full_pattern)
  File "/usr/lib/python3/dist-packages/pexpect/spawnbase.py", line 372, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/lib/python3/dist-packages/pexpect/expect.py", line 169, in expect_loop
    incoming = spawn.read_nonblocking(spawn.maxread, timeout)
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 478, in read_nonblocking
    if does not self.isalive():
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 704, in isalive
    with _wrap_ptyprocess_err():
  File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/usr/lib/python3/dist-packages/pexpect/pty_spawn.py", line 25, in _wrap_ptyprocess_err
    raise ExceptionPexpect(*e.args)
pexpect.exceptions.ExceptionPexpect: isalive() encountered condition where "terminated" is 0, but there was no child process. Did someone else call waitpid() on our process?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 673, in _eval_line
    raise RuntimeError("%s produced error output\n%s\n   executing %s"%(self, error,line))
RuntimeError: Gap produced error output
Error, <size> must be a positive integer

   executing __SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/tmp/ipykernel_115920/3461510396.py", line 85, in dobblegppack
    A = gap(nn).AllSmallGroups()
  File "/usr/lib/python3/dist-packages/sage/interfaces/interface.py", line 693, in __call__
    return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 856, in function_call
    res = self.eval(marker+cmd)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 498, in eval
    result = Expect.eval(self, input_line, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/expect.py", line 1382, in eval
    return '\n'.join([self._eval_line(L, allow_use_file=allow_use_file, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/expect.py", line 1382, in <listcomp>
    return '\n'.join([self._eval_line(L, allow_use_file=allow_use_file, **kwds)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 703, in _eval_line
    return self._eval_line(line, allow_use_file=allow_use_file)
  File "/usr/lib/python3/dist-packages/sage/interfaces/gap.py", line 707, in _eval_line
    raise RuntimeError(exc)
RuntimeError: Gap produced error output
Error, <size> must be a positive integer

   executing __SAGE_LAST__:="__SAGE_LAST__";;AllSmallGroups(\$sage1);;
change anything
    else:
        print("Completed with", i, "at", time.localtime())
        cancel_alarm()