CodeKingPlusPlus
CodeKingPlusPlus

Reputation: 16081

Python and Scheduling Computation

I wish to schedule a computation to occur after my current computation in Python is finished. Note that my Python interpreter is running through emacs.

For example I am currently running:

>>> for i in range(2, 5):
...     tn.TweetNetwork.create_subnetworks(i)
... 

I made a simple mistake and meant to type range(1,5). This has been running for at least 4 hours and should run for another few hours. That being said I do not want to re-execute the loop with the correction and lose all that has been computed.

As I am not by the computer 24/7, how can I schedule Python to execute the function `tn.TweetNetwork.create_subnetworks(1)?

I use emacs 24.3 and ubuntu 12.04 LTS, let me know if you need more information. All help is greatly appreciated!

EDIT: I like the answer posted, however I do not know how to find the PID. I am running a Python interpreter through emacs. So how would I find that out?

Upvotes: 1

Views: 240

Answers (3)

juanleon
juanleon

Reputation: 9400

If Tyler comment does not help, you may eval the following piece of code:

(defun foo (ignored)
  (remove-hook 'comint-output-filter-functions 'foo)
  (run-with-timer 1 nil (lambda()
                          (goto-char (point-max))
                          (insert "tn.TweetNetwork.create_subnetworks(1)")
                          (comint-send-input))))

(add-hook 'comint-output-filter-functions 'foo)

It defines a function that will insert the command you need to insert in the python inferior buffer, a second after the invocation of that function (the delay is for avoid recursive loops).

Then it setup the invocation of that function upon the event where the inferior process (python, in your case) writes anything. In your case, that would be the ">>>" prompt, that python writes when ready. If your code is generating output, this approach won't work.

If you are using comint in other buffers (shell, sql, ...) you would need to make variable comint-output-filter-functions local to your python interactive buffer (with make-variable-buffer-local)

Upvotes: 0

user797257
user797257

Reputation:

This was too much for the comment, but this isn't a complete reply.

To get a process started by Emacs:

  1. M-x list-processes,
  2. identify the process you want to get the id of
  3. M-:(process-id (get-process "name-of-the-process")).

But this will give you the process of the interpreter, not any other process started from it.

If you then need to get all processes spawned through that process, you can do:

$ pstree PID

Where PID is the one you obtained earlier from Emacs.

Upvotes: 1

Vladimir Lagunov
Vladimir Lagunov

Reputation: 1905

I think, the easiest way is to write another script that wait until your process finished and runs tn.TweetNetwork.create_subnetworks(1). This will work only if your create_subnetworks does not access any global variables and does and write all results into database/file/etc.

# Write script similar to these
import os, time

print "Wait until old script completed..."
while os.path.exists("/proc/SCRIPT_PID"):
    time.sleep(1)

print "Execute create_subnetworks..."
tn = ...
tn.TweetNetwork.create_subnetworks(1)

Connect to your computer by SSH, get process id by ps axu | grep script_name and run this new script.

Upvotes: 0

Related Questions