Ask Your Question
3

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

asked 2010-08-24 17:01:04 +0100

ccanonc gravatar image

updated 2015-01-14 09:53:07 +0100

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

Comments

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

ccanonc gravatar imageccanonc ( 2010-08-27 12:12:19 +0100 )edit

2 Answers

Sort by » oldest newest most voted
7

answered 2010-08-24 17:57:36 +0100

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

Comments

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 18:42:40 +0100 )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 18:45:11 +0100 )edit

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

William Stein gravatar imageWilliam Stein ( 2010-08-24 21:34:39 +0100 )edit
3

answered 2010-08-24 17:04:05 +0100

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

Comments

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

ccanonc gravatar imageccanonc ( 2010-08-24 17:39:31 +0100 )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 17:48:06 +0100 )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 17:51:15 +0100 )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 22:00:15 +0100 )edit

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

Mike Hansen gravatar imageMike Hansen ( 2010-08-24 22:12:49 +0100 )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

Stats

Asked: 2010-08-24 17:01:04 +0100

Seen: 6,081 times

Last updated: Aug 24 '10