Skip to main content

Next-generation Jupyter kernel management with decoupled client connections

Project description

nextgen-kernels-api

A next-generation Jupyter kernel client architecture that enables shared kernel connections and centralized message routing.

Motivation

In the upstream Jupyter Server implementation, each WebSocket connection establishes its own set of ZMQ sockets to the kernel:

graph LR
    WS1[WebSocket 1] --> ZMQ1[ZMQ Client 1]
    WS2[WebSocket 2] --> ZMQ2[ZMQ Client 2]
    WS3[WebSocket 3] --> ZMQ3[ZMQ Client 3]
    ZMQ1 --> K[Kernel]
    ZMQ2 --> K
    ZMQ3 --> K

Problems:

  • Resource overhead: Multiple redundant ZMQ connections per kernel
  • State fragmentation: No centralized view of kernel execution state
  • Lost messages: No way to route kernel messages to server-side consumers (YDocs, etc.)

Architecture

This project introduces a shared kernel client managed by the kernel manager itself:

graph TB
    subgraph Consumers
        WS1[WebSocket 1]
        WS2[WebSocket 2]
        WS3[WebSocket 3]
        YD[YDoc / Server Documents]
    end

    subgraph "Kernel Manager"
        KC[Shared Kernel Client]
    end

    WS1 -->|register as listener| KC
    WS2 -->|register as listener| KC
    WS3 -->|register as listener| KC
    YD -.->|route messages directly| KC

    KC <-->|single ZMQ connection| K[Kernel]

    style YD stroke-dasharray: 5 5

Client Lifecycle

sequenceDiagram
    participant WS as WebSocket
    participant KM as Kernel Manager
    participant KC as Kernel Client
    participant K as Kernel

    Note over KM,KC: Kernel start creates client
    KM->>KC: create kernel_client instance
    KM->>K: start kernel
    KM->>KC: load connection info
    KM->>KC: connect()
    KC->>K: test communication
    KC->>KC: mark connection ready

    WS->>KC: WebSocket connects
    KC->>WS: add_listener(websocket_callback)
    KC-->>WS: broadcast current state

    WS->>KC: send message to kernel
    K-->>KC: kernel response
    KC-->>WS: route to all listeners
    KC-->>YD: route to YDoc (if registered)

Key Design Principles

  1. Single Client per Kernel: One shared ZMQ connection per kernel, used by all consumers
  2. Listener Pattern: WebSockets and server-side components register as message listeners
  3. Centralized State: Track execution state, activity, and lifecycle from one place
  4. Message Queuing: Queue messages during startup, deliver when connection ready
  5. Server-Side Routing: Enable direct message flow to YDocs for accurate state tracking

Features

  • Shared Kernel Client: Single ZMQ connection per kernel, shared across all consumers
  • Message Listener API: Register callbacks to receive kernel messages from all channels
  • Message Filtering: Filter messages by type and channel when adding listeners (see docs)
  • Configurable WebSocket Filtering: Configure message filtering for WebSocket connections via Jupyter Server config (see docs)
  • Connection Management: Robust connect/disconnect/reconnect with health checks
  • State Tracking: Monitor execution state (idle, busy, starting) via status messages
  • Message Queuing: Queue messages during connection setup, deliver when ready
  • Message Cache: Track outgoing requests and map responses to source channels

Integration with Jupyter Server Documents

This architecture is designed to work seamlessly with jupyter-server-documents, enabling server-side YDocs to receive kernel messages directly:

Benefits:

  • Accurate execution state: YDoc always knows if kernel is busy/idle
  • No lost outputs: Cell outputs flow directly to YDoc, even if no WebSocket connected
  • Real-time collaboration: All clients (WebSockets + YDoc) see kernel state simultaneously

To integrate, simply register the YDoc as a listener on the kernel manager's client:

# In your YDoc initialization or extension
# Get the multi-kernel manager from the server app
mkm = app.kernel_manager

# Get the specific kernel manager for your kernel
km = mkm.get_kernel(kernel_id)

# Access the shared kernel client
client = km.kernel_client

# Listen to all messages
client.add_listener(ydoc.handle_kernel_message)

# Or filter to only specific message types
client.add_listener(
    ydoc.handle_kernel_message,
    msg_types=[("execute_result", "iopub"), ("stream", "iopub")]
)

Installation

pip install nextgen-kernels-api

Enable the extension:

jupyter server extension enable nextgen_kernels_api

This extension will automatically override the default Jupyter Server kernel APIs when the server starts.

License

BSD-3-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nextgen_kernels_api-0.9.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nextgen_kernels_api-0.9.0-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file nextgen_kernels_api-0.9.0.tar.gz.

File metadata

  • Download URL: nextgen_kernels_api-0.9.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for nextgen_kernels_api-0.9.0.tar.gz
Algorithm Hash digest
SHA256 8ebb0b837d93e6c4abbdcf70600614f1815b298e40cda88923f01048fd6f007e
MD5 7fbdb8da6e4eab327fedefac621209c2
BLAKE2b-256 4839acab1c38f969fe5e43a7369e06647aab4c7cca4e12889ba16afa1cc65bb5

See more details on using hashes here.

File details

Details for the file nextgen_kernels_api-0.9.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nextgen_kernels_api-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed34c3489a9a55577dd8e68f4049f159d4c7fc5fa1235f1e0d321f5a751ead2d
MD5 7ede4a9c57688b584d720d854d9ac687
BLAKE2b-256 487710457ef73021562cdbf6add5ee9d9b2e8971233a2b6c0a0f7df53810c5c5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page