emacs-jupyter / jupyter

An interface to communicate with Jupyter kernels.
GNU General Public License v3.0
933 stars 92 forks source link

Is it possible to kill remote kernels #213

Open jkitchin opened 4 years ago

jkitchin commented 4 years ago

Hi,

I have worked out how to run a remote kernel at NERSC via ssh. First, I run

ssh jkitchin@nersc "jupyter kernel --kernel=myenv" which creates a kernel,

and then I connect to it like this

+BEGIN_SRC jupyter-python :session /ssh:nersc:/global/u2/j/jkitchin/.local/share/jupyter/runtime/kernel-13bd20a9-e77f-411a-ba13-c33526716961.json

import sys print(sys.path)

+END_SRC

this works, but I wondered if there is a way to kill this kernel remotely. It turns out that just killing the ssh command does not do that, it leaves the kernel running on the server.

update: ssh -t jkitchin@nersc "jupyter kernel --kernel=myenv" does kill the kernel when I kill the ssh command.

I still wonder if it could be done by elisp in jupyter.el

dangom commented 4 years ago

Hi John,

I don't know if this fits your workflow, but you can start a remote kernel directly without the need for the external ssh via:

#+BEGIN_SRC jupyter-python :session /ssh:nersc:sessioname
import sys
print(sys.path)
#+END_SRC

Then if you switch to the REPL you can kill the buffer and emacs-jupyter will prompt whether you'd like to shut down the remote kernel. You can also press C-c h on a jupyter-python block and then i to interrupt it.

nnicandro commented 4 years ago

What @dangom said is, I think, a better method if you are able to start your own kernel processes.

You should be able to shutdown a kernel that you connect to with a JSON file like this

(jupyter-org-with-src-block-client
   (let ((client jupyter-current-client))
     (jupyter-wait-until-idle
      (let ((jupyter-inhibit-handlers '(:shutdown-reply)))
        (jupyter-send-shutdown-request client)))
     (jupyter-stop-channels client)))

point has to be inside the source block, not on the #+begin_src ... line.

The jupyter-inhibit-handlers bit may be needed to suppress this function from running when a :shutdown-reply is received by the client. I don't think that function is implemented to handle this case.

The client would still be around though and gets cleaned up only when the REPL buffer is killed. There isn't an easy way to do the cleanup right now, but the code is in org-babel-jupyter-initiate-session-by-key.