Jupyter Client
Jupyter Client (`jupyter_client`) provides the reference implementation of the Jupyter protocol, offering Python APIs for starting, managing, and communicating with Jupyter kernels. It also includes the `jupyter kernelspec` entrypoint for installing kernel specifications. Currently at version 8.8.0, the library maintains an active development pace with several releases annually to introduce enhancements and bug fixes.
Warnings
- breaking Jupyter Client 7.0 introduced significant internal API changes for `KernelManager` and `AsyncKernelManager`. Methods like `pre_start_kernel`, `_launch_kernel`, `_kill_kernel`, and `finish_shutdown` had their signatures or return types altered. While primarily affecting subclasses, these changes can impact custom kernel managers.
- breaking With Jupyter Client 7.0, `AsyncKernelClient` methods (e.g., `get_shell_msg`, `get_iopub_msg`, `get_stdin_msg`, `get_control_msg`) no longer accept the `block: bool = True` keyword argument. These methods are now purely asynchronous.
- gotcha Reliably receiving all messages from a kernel, especially output streams (IOPub), can be challenging. Developers often miss messages or get stuck in infinite loops if message processing and timeout handling are not robustly implemented.
- gotcha Improper management of kernel lifecycles can lead to zombie processes, leaked resources, or stale connection files. Failing to shut down kernels and stop client channels can consume system resources.
Install
-
pip install jupyter-client
Imports
- KernelManager
from jupyter_client import KernelManager
- AsyncKernelManager
from jupyter_client.manager import AsyncKernelManager
- BlockingKernelClient
from jupyter_client.blocking import BlockingKernelClient
- find_connection_file
from jupyter_client import find_connection_file
Quickstart
import time
from jupyter_client import KernelManager
from queue import Empty
def main():
# Start a Python kernel
km = KernelManager(kernel_name='python3')
km.start_kernel()
# Get a blocking client to interact with the kernel
kc = km.client(blocking=True)
kc.start_channels()
try:
# Ensure the client is connected and ready
kc.wait_for_ready(timeout=60)
# Execute some code
code = 'print("Hello from Jupyter Kernel!")\na = 10\nprint(f"a is {a}")'
msg_id = kc.execute(code)
# Process IOPub messages until execution state is idle or timeout
while True:
try:
msg = kc.get_iopub_msg(timeout=1)
msg_type = msg['msg_type']
content = msg['content']
if msg_type == 'stream' and content['name'] == 'stdout':
print(f"[Kernel Output]: {content['text'].strip()}")
elif msg_type == 'status' and content['execution_state'] == 'idle':
print("[Kernel Status]: Idle")
break
except Empty:
# No messages in queue, continue waiting for idle status
continue
except Exception as e:
print(f"Error receiving IOPub message: {e}")
break
finally:
# Clean up: stop channels and shut down the kernel
kc.stop_channels()
km.shutdown_kernel()
print("Kernel shutdown complete.")
if __name__ == '__main__':
main()