Ask Your Question

Can I pause a computation rather than aborting it? (Interrupt Command)

asked 2010-08-24 10:01:04 -0500

ccanonc gravatar image

updated 2015-01-14 02:53:07 -0500

FrédéricC gravatar image

Can I pause a computation rather than aborting it?

Sometimes I don't want to discard work by raising an exception. It would be nice if I could just pause execution ad hoc.

edit retag flag offensive close merge delete


Harder: It would be nice if I could inspect the paused state too.

ccanonc gravatar imageccanonc ( 2010-08-27 05:12:19 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2010-08-24 10:57:36 -0500

William Stein gravatar image

You can find the PID of the working process by typing os.getpid() in the worksheet. So to do what you want now, try the following:

  1. In the first worksheet (the one you may want to pause), type os.getpid() and note the PID.
  2. Start something that will take a long time (e.g., factor(2^997-1)) going in the first worksheet.
  3. In a second worksheet, type import signal; os.kill(that pid above, signal.SIGSTOP). The first worksheet will appear to be running, but will not use any CPU resources, as you can confirm with top on the server.
  4. Now in the second worksheet, type os.kill(that pid above, signal.SIGCONT). You will see in top that the Python process associated with the first worksheet is back and burning resources.

The point of the above exercise is that it illustrates that a pause feature could fully work. To really do this right, one would need to add something to the Sage Notebook itself, e.g., modify the sagenb spkg. This would involve some GUI design choices, etc.

edit flag offensive delete link more


Does the worksheet process do the spawning then (is it the parent)? If so, the PID could be displayed by default for worksheets (since a person might not call os.getpid() first, by forgetting). Or does twisted spawn the processes? Neither? In any case it would be a cool project.

ccanonc gravatar imageccanonc ( 2010-08-24 11:42:40 -0500 )edit

A related project might be accounting for spawned time. I've noticed that "%time" sometimes puts almost everything in Wall time, which strictly speaking is correct, but not as interesting as a 3-way breakdown. A more difficult project perhaps.

ccanonc gravatar imageccanonc ( 2010-08-24 11:45:11 -0500 )edit

Twisted spawns the worksheet process. It can determine the PID of that process.

William Stein gravatar imageWilliam Stein ( 2010-08-24 14:34:39 -0500 )edit

answered 2010-08-24 10:04:05 -0500

Mike Hansen gravatar image

There currently isn't any way to pause a computation in the notebook. The only way I can think of the implement such a thing would be to send a SIGTSTP signal to the worker process, but I do not know how the pexpect interface to that process would behave.

edit flag offensive delete link more


How would I find the proper parent process to send a cascade of SIGTSTPs?

ccanonc gravatar imageccanonc ( 2010-08-24 10:39:31 -0500 )edit

Mike -- this is a very good idea. I think it would be likely to work. ccanonc -- as you probably know, on the command line you can just hit "control-Z" to pause, and "fg" to start again.

William Stein gravatar imageWilliam Stein ( 2010-08-24 10:48:06 -0500 )edit

When I say "likely to work", I mean "with a bunch of work, eventually it could be made to work".

William Stein gravatar imageWilliam Stein ( 2010-08-24 10:51:15 -0500 )edit

Mike: I wish I could mark both answers as favorite, but I don't think I can. I think it would be a good askbot enhancement: to have a max threshold. I'll comment on that Q&A thread.

ccanonc gravatar imageccanonc ( 2010-08-24 15:00:15 -0500 )edit

I thought that William's was better than mine :-)

Mike Hansen gravatar imageMike Hansen ( 2010-08-24 15:12:49 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2010-08-24 10:01:04 -0500

Seen: 1,830 times

Last updated: Aug 24 '10