jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

Server extension interfacing with kernels #337

Open SimonBiggs opened 6 years ago

SimonBiggs commented 6 years ago

What is the recommended way for a jupyter server extension to directly interact with kernels?

I tried it naively initially just using the rest api and it just resulted in the server locking up. I imagine this was because it was waiting for a server response but it was talking to itself and it couldn't respond until it stopped waiting.

So, as a test I simply made another thread using multithreading toolbox. This then fixed it.

But my suspicion is that this wouldn't be the right way to go about it...

Hence this question. Should I use the rest API even when I am calling the API from the server itself? Is there a better way to go about it?

My use case is that I want to make a handler which when called with a code string causes that code to only run when all users have disconnected.

takluyver commented 6 years ago

I think your guess about the REST API locking up is right. You probably need to access the kernel more directly, through application.kernel_manager. Unfortunately I don't know of any good example extensions to point you to for this. The notebook server doesn't really have a documented Python API at the moment.

SimonBiggs commented 6 years ago

Thanks @takluyver. If I spawned a new thread that did work (calling the rest API that is). It's quite round about but my thought is that by using the rest API at least my code is less likely to break on notebook server upgrades given the extra stability gained from using a published public API. Is that preference misplaced? Are the internal APIs also quite stable?

Would using the multithreading toolbox to spawn a thread that calls the rest API when that handler is called be okay? Or should I stay away from another thread as much as possible?