Implementation of the Kernel object.

Notes

Messages from the server are handled in the order they were received and asynchronously. Any message handler can return a promise, and message handling will pause until the promise is fulfilled.

Implements

Constructors

Properties

_clientId: string
_comms: Map<string, IComm> = ...
_connectionStatus: Kernel.ConnectionStatus = 'connecting'
_connectionStatusChanged: Signal<KernelConnection, Kernel.ConnectionStatus> = ...
_displayIdToParentIds: Map<string, string[]> = ...
_disposed: Signal<KernelConnection, void> = ...
_futures: Map<string, KernelFutureHandler<IShellControlMessage, IShellControlMessage>> = ...
_hasPendingInput: boolean = false
_id: string = ''
_info: PromiseDelegate<KernelMessage.IInfoReply> = ...
_isDisposed: boolean = false
_kernelSession: string = ''
_model: undefined | Kernel.IModel
_msgChain: Promise<void> = ...
_msgIdToDisplayIds: Map<string, string[]> = ...
_name: string = ''
_pendingInput: Signal<KernelConnection, boolean> = ...
_reason: string = ''
_reconnectAttempt: number = 0
_reconnectLimit: number = 7
_reconnectTimeout: any = null
_selectedProtocol: string = ''
_specPromise: Promise<undefined | ISpecModel>
_status: Status = 'unknown'
_statusChanged: Signal<KernelConnection, Status> = ...
_supportedProtocols: string[] = ...
_targetRegistry: {
    [key: string]: ((comm, msg) => void);
} = ...

Type declaration

  • [key: string]: ((comm, msg) => void)
_username: string = ''
_ws: null | WebSocket = null

Websocket to communicate with kernel.

handleComms: boolean

Handle comm messages

Notes

The comm message protocol currently has implicit assumptions that only one kernel connection is handling comm messages. This option allows a kernel connection to opt out of handling comms.

See https://github.com/jupyter/jupyter_client/issues/263

serverSettings: ServerConnection.ISettings

The server settings for the kernel.

Accessors

Methods

  • Check to make sure it is okay to proceed to handle a message.

    Notes

    Because we handle messages asynchronously, before a message is handled the kernel might be disposed or restarted (and have a different session id). This function throws an error in each of these cases. This is meant to be called at the start of an asynchronous message handler to cancel message processing if the message no longer is valid.

    Returns void

  • Forcefully clear the socket state.

    Notes

    This will clear all socket state without calling any handlers and will not update the connection status. If you call this method, you are responsible for updating the connection status as needed and recreating the socket if you plan to reconnect.

    Returns void

  • Send a message on the websocket.

    If queue is true, queue the message for later sending if we cannot send now. Otherwise throw an error.

    Notes

    As an exception to the queueing, if we are sending a kernel_info_request message while we think the kernel is restarting, we send the message immediately without queueing. This is so that we can trigger a message back, which will then clear the kernel restarting state.

    Parameters

    Returns void

  • Handles a kernel shutdown.

    Notes

    This method should be called if we know from outside information that a kernel is dead (for example, we cannot find the kernel model on the server).

    Returns void

  • Register a comm target handler.

    Parameters

    • targetName: string

      The name of the comm target.

    • callback: ((comm, msg) => void | PromiseLike<void>)

      The callback invoked for a comm open message.

        • (comm, msg): void | PromiseLike<void>
        • Parameters

          Returns void | PromiseLike<void>

    Returns void

    A disposable used to unregister the comm target.

    Notes

    Only one comm target can be registered to a target name at a time, an existing callback for the same target name will be overridden. A registered comm target handler will take precedence over a comm which specifies a target_module.

    If the callback returns a promise, kernel message processing will pause until the returned promise is fulfilled.

  • Register an IOPub message hook.

    Parameters

    • msgId: string
    • hook: ((msg) => boolean | PromiseLike<boolean>)

      The callback invoked for the message.

      Notes

      The IOPub hook system allows you to preempt the handlers for IOPub messages that are responses to a given message id.

      The most recently registered hook is run first. A hook can return a boolean or a promise to a boolean, in which case all kernel message processing pauses until the promise is fulfilled. If a hook return value resolves to false, any later hooks will not run and the function will return a promise resolving to false. If a hook throws an error, the error is logged to the console and the next hook is run. If a hook is registered during the hook processing, it will not run until the next message. If a hook is removed during the hook processing, it will be deactivated immediately.

      See also [[IFuture.registerMessageHook]].

    Returns void

  • Remove a comm target handler.

    Parameters

    • targetName: string

      The name of the comm target to remove.

    • callback: ((comm, msg) => void | PromiseLike<void>)

      The callback to remove.

      Notes

      The comm target is only removed if the callback argument matches.

        • (comm, msg): void | PromiseLike<void>
        • Parameters

          Returns void | PromiseLike<void>

    Returns void

  • Send an execute_request message.

    Notes

    See Messaging in Jupyter.

    Future onReply is called with the execute_reply content when the shell reply is received and validated. The future will resolve when this message is received and the idle iopub status is received. The future will also be disposed at this point unless disposeOnDone is specified and false, in which case it is up to the caller to dispose of the future.

    See also: [[IExecuteReply]]

    Parameters

    • content: {
          allow_stdin?: boolean;
          code: string;
          silent?: boolean;
          stop_on_error?: boolean;
          store_history?: boolean;
          user_expressions?: JSONObject;
      }
      • Optional allow_stdin?: boolean

        Whether to allow stdin requests. The default is true.

      • code: string

        The code to execute.

      • Optional silent?: boolean

        Whether to execute the code as quietly as possible. The default is false.

      • Optional stop_on_error?: boolean

        Whether to the abort execution queue on an error. The default is false.

      • Optional store_history?: boolean

        Whether to store history of the execution. The default true if silent is False. It is forced to false if silent is true.

      • Optional user_expressions?: JSONObject

        A mapping of names to expressions to be evaluated in the kernel's interactive namespace.

    • disposeOnDone: boolean = true
    • Optional metadata: JSONObject

    Returns IShellFuture<IExecuteRequestMsg, IExecuteReplyMsg>

  • Request a kernel restart.

    Notes

    Uses the Jupyter Server API and validates the response model.

    Any existing Future or Comm objects are cleared once the kernel has actually be restarted.

    The promise is fulfilled on a valid server response (after the kernel restarts) and rejected otherwise.

    It is assumed that the API call does not mutate the kernel id or name.

    The promise will be rejected if the request fails or the response is invalid.

    Returns Promise<void>