ipython / ipykernel

IPython Kernel for Jupyter
https://ipykernel.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
649 stars 367 forks source link

How can I detect from labextension whether kernel is still alive? #428

Open bernhard-42 opened 5 years ago

bernhard-42 commented 5 years ago

I use ipykernel 5.1.2 on a remote machine via ssh with local comm ports (created by jupyter lab 1.1.1) mapped to remote comm ports (created via jupyter_client.write_connection_file) via ssh port forwarding.

Everything works perfectly fine. Now, my remote machine can be stopped externally (auto termination of remote platform). I don't find a reasonable way to detect from javascript (in my labextension) whether the python kernel is really alive. None of the status or alive methods detect a vanished kernel. The last idea I had was to use something like

f = s.kernel.requestExecute({code:"pass", silent:true})
f.onReply = handler

to force python execution (when returning immediatley/soon, I am sure the kernel runs)

However, sound like pretty heavy.

Is there an easier way that reliably detects kernel death?

Thanks, Bernhard

bernhard-42 commented 5 years ago

After some more thinking, requestExecute turned out to be a not very smart idea for detecting whether a remote kernel is alive. Is there any jupyter / ipykernel way to get the alive state?

edisongustavo commented 5 years ago

The Kernel has a heartbeat thread which you can use to inspect if it is still alive.

If you're using python and the jupyter_client, then this should work:

from jupyter_client import BlockingKernelClient
client = BlockingKernelClient()
client.load_connection_file("path-to-connections-file.json")
client.start_channels()
print(client.is_alive())

You can check the implementation of is_alive here: https://github.com/jupyter/jupyter_client/blob/59a74df2b59d5f575f513b070f8c5f765c392b40/jupyter_client/client.py#L182

Would this be enough for your needs?

bernhard-42 commented 5 years ago

Thanks @edisongustavo that works from python. To now get the info from the labextension (jJavascript world), I would need to implement a server extension that gets queried by the labextension? Or would you know a direct way in javascript (I would have expected the frontend to be connected, however using is_alive on the Javascript kernel object always returns True, even when disconnected)

edisongustavo commented 5 years ago

Or would you know a direct way in javascript.

In that case I don't know. I've never touched the Javascript side of Jupyter. Sorry about that.