Options
All
  • Public
  • Public/Protected
  • All
Menu

Class DefaultKernel

Implementation of the Kernel object. The namespace for DefaultKernel statics.

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.

Hierarchy

  • DefaultKernel

Implements

Index

Constructors

constructor

Properties

Private _anyMessage

_anyMessage: Signal<this, IAnyMessageArgs> = new Signal<this, Kernel.IAnyMessageArgs>(this)

Private _clientId

_clientId: string = ""

Private _comms

_comms: Map<string, IComm> = new Map<string, Kernel.IComm>()

Private _displayIdToParentIds

_displayIdToParentIds: Map<string, string[]> = new Map<string, string[]>()

Private _futures

_futures: Map<string, KernelFutureHandler<IShellMessage<"comm_close" | "comm_msg" | "comm_open" | "comm_info_reply" | "comm_info_request" | "complete_reply" | "complete_request" | "execute_reply" | "execute_request" | "history_reply" | "history_request" | "inspect_reply" | "inspect_request" | "interrupt_reply" | "interrupt_request" | "is_complete_reply" | "is_complete_request" | "kernel_info_reply" | "kernel_info_request" | "shutdown_reply" | "shutdown_request"> | IControlMessage<never>, IShellMessage<"comm_close" | "comm_msg" | "comm_open" | "comm_info_reply" | "comm_info_request" | "complete_reply" | "complete_request" | "execute_reply" | "execute_request" | "history_reply" | "history_request" | "inspect_reply" | "inspect_request" | "interrupt_reply" | "interrupt_request" | "is_complete_reply" | "is_complete_request" | "kernel_info_reply" | "kernel_info_request" | "shutdown_reply" | "shutdown_request"> | IControlMessage<never>>> = new Map<string,KernelFutureHandler<KernelMessage.IShellControlMessage,KernelMessage.IShellControlMessage>>()

Private _id

_id: string = ""

Private _info

_info: IInfoReply | null = null

Private _initialized

_initialized: boolean = false

Private _iopubMessage

_iopubMessage: Signal<this, IIOPubMessage<"clear_output" | "comm_close" | "comm_msg" | "comm_open" | "display_data" | "error" | "execute_input" | "execute_result" | "status" | "stream" | "update_display_data">> = new Signal<this, KernelMessage.IIOPubMessage>(this)

Private _isDisposed

_isDisposed: boolean = false

Private _isReady

_isReady: boolean = false

Private _kernelSession

_kernelSession: string = ""

Private _msgChain

_msgChain: Promise<void> | null = Promise.resolve()

Private _msgIdToDisplayIds

_msgIdToDisplayIds: Map<string, string[]> = new Map<string, string[]>()

Private _name

_name: string = ""

Private _pendingMessages

_pendingMessages: IMessage[] = []

Private _readyPromise

_readyPromise: PromiseDelegate<void> = new PromiseDelegate<void>()

Private _reconnectAttempt

_reconnectAttempt: number = 0

Private _reconnectLimit

_reconnectLimit: number = 7

Private _specPromise

_specPromise: Promise<ISpecModel>

Private _status

_status: Status = "unknown"

Private _statusChanged

_statusChanged: Signal<this, "unknown" | "starting" | "reconnecting" | "idle" | "busy" | "restarting" | "autorestarting" | "dead" | "connected"> = new Signal<this, Kernel.Status>(this)

Private _targetRegistry

_targetRegistry: object = Object.create(null)

Type declaration

Private _terminated

_terminated: Signal<this, void> = new Signal<this, void>(this)

Private _unhandledMessage

_unhandledMessage: Signal<this, IMessage<"clear_output" | "comm_close" | "comm_msg" | "comm_open" | "display_data" | "error" | "execute_input" | "execute_result" | "status" | "stream" | "update_display_data" | "comm_info_reply" | "comm_info_request" | "complete_reply" | "complete_request" | "execute_reply" | "execute_request" | "history_reply" | "history_request" | "inspect_reply" | "inspect_request" | "interrupt_reply" | "interrupt_request" | "is_complete_reply" | "is_complete_request" | "kernel_info_reply" | "kernel_info_request" | "shutdown_reply" | "shutdown_request" | "input_request" | "input_reply">> = new Signal<this, KernelMessage.IMessage>(this)

Private _username

_username: string = ""

Private _ws

_ws: WebSocket | null = null

Private _wsStopped

_wsStopped: boolean = false

serverSettings

serverSettings: ISettings

The server settings for the kernel.

Accessors

anyMessage

  • A signal emitted for any kernel message.

    Notes

    This signal is emitted when a message is received, before it is handled asynchronously.

    The behavior is undefined if the message is modified during message handling. As such, the message should be treated as read-only.

    Returns ISignal<this, IAnyMessageArgs>

clientId

  • get clientId(): string

id

  • get id(): string

info

iopubMessage

  • A signal emitted for iopub kernel messages.

    Notes

    This signal is emitted after the iopub message is handled asynchronously.

    Returns ISignal<this, IIOPubMessage>

isDisposed

  • get isDisposed(): boolean

isReady

  • get isReady(): boolean

model

name

  • get name(): string

ready

  • get ready(): Promise<void>
  • A promise that is fulfilled when the kernel is ready.

    Returns Promise<void>

status

statusChanged

  • get statusChanged(): ISignal<this, Status>

terminated

  • get terminated(): ISignal<this, void>
  • A signal emitted when the kernel is shut down.

    Returns ISignal<this, void>

unhandledMessage

  • get unhandledMessage(): ISignal<this, IMessage>
  • A signal emitted for unhandled kernel message.

    Notes

    This signal is emitted for a message that was not handled. It is emitted during the asynchronous message handling code.

    Returns ISignal<this, IMessage>

username

  • get username(): string

Methods

Private _assertCurrentMessage

  • _assertCurrentMessage(msg: IMessage): void
  • 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.

    Parameters

    Returns void

Private _clearSocket

  • _clearSocket(): void
  • Clear the socket state.

    Notes

    When calling this, you should also set the status to something like 'reconnecting' to reset the kernel ready state.

    Returns void

Private _clearState

  • _clearState(): Promise<void>

Private _createSocket

  • _createSocket(): void
  • Create the kernel websocket connection and add socket status handlers.

    Returns void

Private _handleCommClose

Private _handleCommMsg

Private _handleCommOpen

Private _handleDisplayId

  • _handleDisplayId(displayId: string, msg: IMessage): Promise<boolean>
  • Handle a message with a display id.

    Parameters

    Returns Promise<boolean>

    Whether the message was handled.

Private _handleMessage

  • _handleMessage(msg: IMessage): Promise<void>

Private _noOp

  • _noOp(): void

Private _onWSClose

  • _onWSClose(evt: Event): void

Private _onWSMessage

  • _onWSMessage(evt: MessageEvent): void
  • Handle a websocket message, validating and routing appropriately.

    Parameters

    • evt: MessageEvent

    Returns void

Private _onWSOpen

  • _onWSOpen(evt: Event): void

Private _sendKernelShellControl

Private _sendPending

  • _sendPending(): void

Private _unregisterComm

  • _unregisterComm(commId: string): void

Private _updateStatus

  • _updateStatus(status: Status): void

clone

connectToComm

  • connectToComm(targetName: string, commId?: string): IComm
  • Connect to a comm, or create a new one.

    Notes

    If a client-side comm already exists with the given commId, it is returned.

    Parameters

    • targetName: string
    • Default value commId: string = UUID.uuid4()

    Returns IComm

dispose

  • dispose(): void

getSpec

handleRestart

  • handleRestart(): Promise<void>
  • Handle a restart on the kernel. This is not part of the IKernel interface.

    Returns Promise<void>

interrupt

  • interrupt(): Promise<void>
  • Interrupt a kernel.

    Notes

    Uses the Jupyter Notebook API.

    The promise is fulfilled on a valid response 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 kernel status is Dead or if the request fails or the response is invalid.

    Returns Promise<void>

reconnect

  • reconnect(): Promise<void>
  • Reconnect to a disconnected kernel.

    Notes

    Used when the websocket connection to the kernel is lost.

    Returns Promise<void>

registerCommTarget

  • registerCommTarget(targetName: string, callback: function): void
  • Register a comm target handler.

    Parameters

    • targetName: string

      The name of the comm target.

    • callback: function

      The callback invoked for a comm open message.

    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.

registerMessageHook

  • registerMessageHook(msgId: string, hook: function): void
  • Register an IOPub message hook.

    Parameters

    • msgId: string
    • hook: function

      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

removeCommTarget

  • removeCommTarget(targetName: string, callback: function): void
  • Remove a comm target handler.

    Parameters

    • targetName: string

      The name of the comm target to remove.

    • callback: function

      The callback to remove.

      Notes

      The comm target is only removed the callback argument matches.

    Returns void

removeMessageHook

  • removeMessageHook(msgId: string, hook: function): void
  • Remove an IOPub message hook.

    Parameters

    • msgId: string
    • hook: function

      The callback invoked for the message.

    Returns void

requestCommInfo

  • Send a comm_info_request message.

    Notes

    Fulfills with the comm_info_reply content when the shell reply is received and validated.

    Parameters

    • content: object

    Returns Promise<ICommInfoReplyMsg>

requestComplete

requestExecute

  • 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: object
    • Default value disposeOnDone: boolean = true
    • Optional metadata: JSONObject

    Returns IShellFuture<IExecuteRequestMsg, IExecuteReplyMsg>

requestHistory

requestInspect

requestIsComplete

requestKernelInfo

restart

  • restart(): Promise<void>
  • Restart a kernel.

    Notes

    Uses the Jupyter Notebook API and validates the response model.

    Any existing Future or Comm objects are cleared.

    The promise is fulfilled on a valid response 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>

sendControlMessage

  • Send a control message to the kernel.

    Notes

    Send a message to the kernel's control channel, yielding a future object for accepting replies.

    If expectReply is given and true, the future is disposed when both a control reply and an idle status message are received. If expectReply is not given or is false, the future is resolved when an idle status message is received. If disposeOnDone is not given or is true, the Future is disposed at this point. If disposeOnDone is given and false, it is up to the caller to dispose of the Future.

    All replies are validated as valid kernel messages.

    If the kernel status is dead, this will throw an error.

    Type parameters

    Parameters

    • msg: IControlMessage<T>
    • Default value expectReply: boolean = false
    • Default value disposeOnDone: boolean = true

    Returns IControlFuture<IControlMessage<T>>

sendInputReply

sendShellMessage

  • Send a shell message to the kernel.

    Notes

    Send a message to the kernel's shell channel, yielding a future object for accepting replies.

    If expectReply is given and true, the future is disposed when both a shell reply and an idle status message are received. If expectReply is not given or is false, the future is resolved when an idle status message is received. If disposeOnDone is not given or is true, the Future is disposed at this point. If disposeOnDone is given and false, it is up to the caller to dispose of the Future.

    All replies are validated as valid kernel messages.

    If the kernel status is dead, this will throw an error.

    Type parameters

    Parameters

    • msg: IShellMessage<T>
    • Default value expectReply: boolean = false
    • Default value disposeOnDone: boolean = true

    Returns IShellFuture<IShellMessage<T>>

shutdown

  • shutdown(): Promise<void>
  • Shutdown a kernel.

    Notes

    Uses the Jupyter Notebook API.

    The promise is fulfilled on a valid response and rejected otherwise.

    On a valid response, closes the websocket and disposes of the kernel object, and fulfills the promise.

    If the kernel is already dead, it closes the websocket and returns without a server request.

    Returns Promise<void>

Static connectTo

  • Connect to a running kernel.

    Parameters

    • model: IModel

      The model of the running kernel.

    • Optional settings: ISettings

      The server settings for the request.

    Returns IKernel

    The kernel object.

    Notes

    If the kernel was already started via startNewKernel, the existing Kernel object info is used to create another instance.

Static findById

  • Find a kernel by id.

    Parameters

    • id: string

      The id of the kernel of interest.

    • Optional settings: ISettings

      The optional server settings.

    Returns Promise<IModel>

    A promise that resolves with the model for the kernel.

    Notes

    If the kernel was already started via startNewKernel, we return its Kernel.IModel.

    Otherwise, we attempt to find an existing kernel by connecting to the server. The promise is fulfilled when the kernel is found, otherwise the promise is rejected.

Static getSpecs

Static listRunning

  • Fetch the running kernels.

    Parameters

    • Optional settings: ISettings

      The optional server settings.

    Returns Promise<IModel[]>

    A promise that resolves with the list of running kernels.

    Notes

    Uses the Jupyter Notebook API and validates the response model.

    The promise is fulfilled on a valid response and rejected otherwise.

Static shutdown

  • shutdown(id: string, settings?: ISettings): Promise<void>
  • Shut down a kernel by id.

    Parameters

    • id: string

      The id of the running kernel.

    • Optional settings: ISettings

      The server settings for the request.

    Returns Promise<void>

    A promise that resolves when the kernel is shut down.

Static shutdownAll

  • shutdownAll(settings?: ISettings): Promise<void>
  • Shut down all kernels.

    Parameters

    • Optional settings: ISettings

      The server settings to use.

    Returns Promise<void>

    A promise that resolves when all the kernels are shut down.

Static startNew

  • Start a new kernel.

    Parameters

    • options: IOptions

      The options used to create the kernel.

    Returns Promise<IKernel>

    A promise that resolves with a kernel object.

    Notes

    Uses the Jupyter Notebook API and validates the response model.

    If no options are given or the kernel name is not given, the default kernel will by started by the server.

    Wraps the result in a Kernel object. The promise is fulfilled when the kernel is started by the server, otherwise the promise is rejected.

Generated using TypeDoc