Hey, I want to get kernel state whether it is idle or busy from the local running jupyter notebook. How should I proceed? It would be best If I can get those using command line
Hereās a quick approach that uses the REST API using curl
:
opened 02:06AM - 29 Oct 19 UTC
I've Google'd quite a bit to find a solution for the following use-case.
Esse⦠ntially, my root problem is that I try to find a way to automatically persist an interactive ipython session such that I can restore its state in case a) any client gets disconnected or the jupyter server is stopped, or b) to connect from a different front-end in a different client (might be from completely different device connecting through ssh/mosh, like my iPhone/iPad).
I have also looked into alternatives, such as `dill`, however they require manual saving of the state. Hence, I think having a running ipython kernel on my remote cloud machine is the way to go. So, then, I would need a way to connect multiple front-ends to the same ipython kernel.
These front-ends might be:
- jupyter console
- VSCode interactive
- Jupyter Lab
- Jupyter Notebook
I currently keep a Jupyter server running on my cloud machine, I then connect to the machine using VSCode interactive python (using their new built in way of connecting to Jupyter), which will automatically start a new kernel (I cannot seem to connect to an existing one so this front-end appears to have to be the first to connect).
Jupyter will then log the new kernel id to the terminal, which I can then append to
```bash
jupyter console/notebook --existing kernel-<ID read from terminal output or from %connect_info>.json
```
There is multiple issues here, however.
1) VSCode will disconnect as soon as connectivity is lost and cannot reconnect to the kernel, hence, in 99% of the cases I cannot use `%connect_info`
2) Jupyter notebook logs a lot of info to the terminal, scrolling up in tmux after having the server running for more than 2mins becomes an ordeal
So my questions:
a) Is there a smarter, built-in way to solve my root problem?
b) If not, how do I get a list of running active kernels from a jupyter server so I can connect to them?
Thanks a lot!
You can get the ?token
from e.g.
jupyter server list --json
2 Likes
But it is just providing the List of currently active kernels. I want to see the kernel state when the notebook is not running then I should get the idle message and when it is running I should get the busy message. Can you help me to get this?
The payload from the response includes the executIon_state
⦠i believe this gets cached by the server, and doesnāt actually make a request at that moment.
{'id': '73109856-1658-4abb-b850-6f011325eff5',
'path': 'Untitled.ipynb',
'name': 'Untitled.ipynb',
'type': 'notebook',
'kernel': {'id': '45b29d0c-3a72-416b-a964-7a04f0c637ef',
'name': 'python3',
'last_activity': '2022-07-21T13:39:00.822405Z',
'execution_state': 'idle', # <----- this
'connections': 1},
'notebook': {'path': 'Untitled.ipynb', 'name': 'Untitled.ipynb'}},
Otherwise, you would have to connect to the kernel, with something like this:
import jupyter_core, jupyter_client, pathlib
rt_dir = pathlib.Path(jupyter_core.paths.jupyter_runtime_dir())
cf = next(rt_dir.glob("kernel-*.json")) # do a better job of this
c = jupyter_client.AsyncKernelClient()
c.load_connection_file(cf)
msg = await c.iopub_channel.get_msg()
print(msg["content"]["execution_state"])
1 Like
I am getting this error. {āmessageā: āForbiddenā, āreasonā: null}.
Those instructions were almost certainly from a unix computer⦠it could be your curl
doesnāt work the same way. You could also try in python with subprocess
and urllib
, which should be more portable.
1 Like
Got the required data.
Try this.
payload = {ātokenā: āget token from jupyter notebook listā}
api_url = āhttp://127.0.0.1:8888/api/sessions ā
result = requests.get(api_url,params = payload)
result.json()