Skip to content

kernel

Classes:

  • KernelInterrupt

    Raised to interrupt the kernel.

  • Kernel

    The class containing the handler methods to implement a Jupyter Kernel.

KernelInterrupt

Bases: InterruptedError

Raised to interrupt the kernel.

Source code in src/async_kernel/common.py
45
46
class KernelInterrupt(InterruptedError):
    "Raised to interrupt the kernel."

Kernel

Bases: HasInterface[T_interface_co], LoggingConfigurable, Generic[T_interface_co, T_shell_co]

The class containing the handler methods to implement a Jupyter Kernel.

Methods:

Attributes:

Source code in src/async_kernel/kernel.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
class Kernel(HasInterface[T_interface_co], LoggingConfigurable, Generic[T_interface_co, T_shell_co]):
    """
    The class containing the handler methods to implement a Jupyter Kernel.
    """

    help_links = traitlets.List(trait=traitlets.Dict()).tag(config=True)
    "A list of links provided kernel info request."

    supported_features = traitlets.List(traitlets.Unicode()).tag(config=True)
    "A list of features supported by the kernel."

    handle_in_shell_thread = traitlets.List(
        traitlets.UseEnum(MsgType),
        [MsgType.comm_msg, MsgType.comm_open, MsgType.comm_close],
    ).tag(config=True)
    """
    A list of `MsgType` that are always handled in the shell's thread (typically the _MainThread_).
    """

    handle_in_thread = traitlets.Dict(key_trait=traitlets.UseEnum(MsgType), value_trait=traitlets.Unicode())
    """
    A mapping of `MsgType` to the name of a separate caller (thread) in which to run the handler.
    """

    callers: Fixed[Self, dict] = Fixed(lambda c: c["owner"].parent.callers)
    "A shortcut to the callers dict on the parent."

    caller: Fixed[Self, Caller] = Fixed(lambda c: c["owner"].callers[Channel.shell])
    "The caller for the shell thread."

    debugger = Fixed(Debugger)
    "The debugger for handling debug requests."

    comm_manager = Fixed(CommManager)
    "Creates [async_kernel.comm.Comm][] instances and maintains a mapping to `comm_id` to `Comm` instances."

    active_execute_requests: Fixed[Self, set[Pending[Any]]] = Fixed(set)
    "A set of active execute requests that gets updated by the shell."

    _interrupt_message = "Kernel interrupted"

    _restart = False
    _handler_cache: ClassVar[dict[tuple[str | None, MsgType, Callable], HandlerType]] = {}
    _subshells: dict[str, T_shell_co]
    _interrupt_requested: None | Pending = None

    @traitlets.default("help_links")
    def _default_help_links(self) -> tuple[dict[str, str], ...]:
        return (
            {
                "text": "Async Kernel Reference ",
                "url": "https://fleming79.github.io/async-kernel/",
            },
            {
                "text": "IPython Reference",
                "url": "https://ipython.readthedocs.io/en/stable/",
            },
            {
                "text": "IPython magic Reference",
                "url": "https://ipython.readthedocs.io/en/stable/interactive/magics.html",
            },
            {
                "text": "Matplotlib ipympl Reference",
                "url": "https://matplotlib.org/ipympl/",
            },
            {
                "text": "Matplotlib Reference",
                "url": "https://matplotlib.org/contents.html",
            },
        )

    @traitlets.default("handle_in_thread")
    def _default_handle_in_thread(self) -> dict[MsgType, str]:
        return {
            MsgType.inspect_request: "language_server",
            MsgType.complete_request: "language_server",
            MsgType.is_complete_request: "language_server",
        }

    @traitlets.default("supported_features")
    def _default_supported_features(self) -> list[str]:
        features = ["kernel subshells"]
        if self.debugger.enabled:
            features.append("debugger")
        return features

    @property
    def kernel_info(self) -> dict[str, Any]:
        "Info provided to a kernel info request."
        return {
            "protocol_version": async_kernel.kernel_protocol_version,
            "implementation": async_kernel.distribution_name,
            "implementation_version": async_kernel.__version__,
            "language_info": async_kernel.kernel_protocol_version_info,
            "banner": self.shell.banner,
            "help_links": self.help_links,
            "debugger": self.debugger.enabled,
            "supported_features": self.supported_features,
        }

    @property
    def main_shell(self) -> T_shell_co:
        try:
            return self._main_shell
        except AttributeError:
            msg = "The main_shell is only available once the kernel is started."
            raise RuntimeError(msg) from None

    @property
    def shell(self) -> T_shell_co:
        """
        The shell given the current context.

        Notes:
            - The `subshell_id` of the main shell is `None`.
        """
        return self.get_shell()

    @property
    def subshells(self) -> dict[str, T_shell_co]:
        return self._subshells.copy()

    def __init__(self, parent: T_interface_co, shell_class: type[T_shell_co]) -> None:
        self._subshells = {}
        assert self.parent is parent
        super().__init__()
        self._shell_class = shell_class

    @asynccontextmanager
    async def running(self) -> AsyncGenerator[None]:
        """
        The kernel runs in this context.

        Notes:
            - Entered by the interface (parent).
            - Overload as required.
        """
        remove_patch = self.apply_patches()
        try:
            self._main_shell = self._shell_class(protected=True, is_mainshell=True)
            async with self._main_shell.mainshell_running():
                self.log.info("Kernel started")
                yield
        finally:
            self.log.info("Kernel stopped")
            for subshell in self._subshells.copy().values():
                subshell.stop(force=True)
            for comm in self.comm_manager.comms.copy().values():
                comm.close(deleting=True)
            remove_patch()
            self._handler_cache.clear()

    def _signal_handler(self, signum, frame: FrameType | None) -> None:
        "Handle interrupt signals."

        if pen := self._interrupt_requested:
            self._interrupt_requested = None
            if not pen.done():
                with enable_signal_safety():
                    pen.set_result(None)
                raise KernelInterrupt
        else:
            signal.default_int_handler(signum, frame)

    async def do_interrupt(self) -> None:
        """
        Interrupt/cancel non-silent active execute requests.
        """
        assert Caller() is self.callers[Channel.control], "Must be called from the control thread."
        for pen in self.active_execute_requests.copy():
            if not pen.metadata.get("kwargs", {}).get("silent", False):
                pen.cancel(self._interrupt_message)
        if (
            (sys.platform != "emscripten")
            and (not self.debugger.enabled or not self.debugger.stopped_threads)
            and (caller := Caller("MainThread")).running  # Can only interrupt the main thread
        ):
            self._interrupt_requested = pen = Pending()
            caller.call_direct(lambda: pen.set_result(None))
            try:
                await pen.wait(result=False, timeout=1, protect=True)
            except TimeoutError:
                if sys.platform == "win32":
                    signal.raise_signal(signal.SIGINT)
                else:
                    os.kill(os.getpid(), signal.SIGINT)
                await pen.wait(result=False, timeout=10, protect=True)

    def _patch_signal(self) -> Callable[[], None]:

        with contextlib.suppress(ValueError, AttributeError):
            import signal  # noqa: PLC0415

            sig = signal.signal(signal.SIGINT, self._signal_handler)

            def restore() -> None:
                signal.signal(signal.SIGINT, sig)

            return restore
        return lambda: None

    def apply_patches(self) -> Callable[[], None]:
        """Apply patches returning a callable to reverse the patches."""

        original = sys.displayhook, builtins.input, getpass.getpass
        builtins.input, sys.displayhook, getpass.getpass = self.raw_input, self.displayhook, self.getpass
        restore_comm = self.comm_manager.patch_comm()
        restore_stdout = OutStream("stdout").patch()
        restore_stderr = OutStream("stderr").patch()
        restore_signal = self._patch_signal()

        def restore() -> None:
            sys.displayhook, builtins.input, getpass.getpass = original
            restore_stdout()
            restore_stderr()
            restore_comm()
            restore_signal()

        return restore

    def displayhook(self, result: Any):
        "The global patch for [sys.displayhook][] responsible for python display callbacks."
        self.get_shell().displayhook(result)

    def _shell_created(self, shell: T_shell_co) -> None:  # pyright: ignore[reportGeneralTypeIssues]
        "Called by `BaseShell.__init__`"
        if shell.subshell_id:
            self._subshells[shell.subshell_id] = shell

    def _subshell_stopped(self, shell: T_shell_co) -> None:  # pyright: ignore[reportGeneralTypeIssues]
        "Called by `BaseShell.stop`"
        if subshell_id := shell.subshell_id:
            self._subshells.pop(subshell_id, None)
        for key in list(self._handler_cache):
            if key[0] == subshell_id:
                self._handler_cache.pop(key, None)

    def _get_handler(
        self,
        job: Job,
        send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
        iopub_send: Callable,
    ) -> HandlerType:
        try:
            subshell_id = job["msg"]["content"]["subshell_id"]
        except KeyError:
            try:
                subshell_id = job["msg"]["header"]["subshell_id"]  # pyright: ignore[reportTypedDictNotRequiredAccess]
            except KeyError:
                subshell_id = None
        msg_type = MsgType(job["msg"]["header"]["msg_type"])

        if msg_type is MsgType.execute_request:
            key = (subshell_id, msg_type, send_reply)
        else:
            key = (None, msg_type, send_reply)
        try:
            return self._handler_cache[key]
        except KeyError:
            handler: HandlerType = getattr(self, msg_type)

            @functools.wraps(handler)
            async def run_handler(job: Job) -> None:
                job_token = utils._job_var.set(job)  # pyright: ignore[reportPrivateUsage]
                subshell_token = ShellPendingManager._id_contextvar.set(subshell_id)  # pyright: ignore[reportPrivateUsage]

                try:
                    iopub_send(
                        msg_or_type="status",
                        parent=job["msg"],
                        content={"execution_state": "busy"},
                        ident=b"kernel.status",
                    )
                    if (content := await handler(job)) is not None:
                        await send_reply(job, content)
                except Exception as e:
                    await send_reply(job, utils.error_to_content(e))
                    self.log.exception("Exception in message handler:", exc_info=e)
                finally:
                    utils._job_var.reset(job_token)  # pyright: ignore[reportPrivateUsage]
                    ShellPendingManager._id_contextvar.reset(subshell_token)  # pyright: ignore[reportPrivateUsage]
                    iopub_send(
                        msg_or_type="status",
                        parent=job["msg"],
                        content={"execution_state": "idle"},
                        ident=b"kernel.status",
                    )
                    del job

            self._handler_cache[key] = run_handler
            return run_handler

    def message_handler(
        self,
        job: Job,
        send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
        iopub_send: Callable,
        /,
    ) -> None:
        """
        Schedule handling of the job (msg) with a handler running in a Task managed by a Caller.

        Each `msg_type` runs in a separate task, possibly in a separate thread and event loop.
        Typically, jobs are queued for execution by either the 'shell' or 'control' caller using
        [queue_call][async_kernel.caller.Caller.queue_call].

        'execute_request' messages can also specify alternate run modes:
            - task: Run the execute request as a task.
            - thread: Run the execute request in a worker thread.

            The alternate run mode can be specified in a few ways:
            - as a comment on the first line of the code block `# task` or `# thread`.
            - As a tag `thread` or `task`

        Args:
            job: A dict with the msg and supporting details.
            send_reply: The function for the handler to use to send the reply to the message.
        """
        handler = self._get_handler(job, send_reply, iopub_send)

        run_mode: RunMode | CallerCreateOptions | None = None
        msg_type = MsgType(job["msg"]["header"]["msg_type"])

        if msg_type is MsgType.execute_request:
            caller = self.callers[job["msg"]["channel"]]
            try:
                run_mode = next(mode for tag in utils.get_tags(job) if (mode := RunMode.to_runmode(tag)))
            except StopIteration:
                if content := job["msg"].get("content", {}):
                    if (code := content.get("code")) and (
                        mode := RunMode.to_runmode(code.strip().split("\n", maxsplit=1)[0])
                    ):
                        run_mode = mode
                    if content.get("silent"):
                        run_mode = RunMode.task

        elif msg_type in self.handle_in_shell_thread:
            caller = self.callers[Channel.shell]
        else:
            caller = self.callers[Channel.control]
            if thread_name := self.handle_in_thread.get(msg_type):
                caller = caller.get(name=thread_name, no_debug=True)

        match run_mode:
            case RunMode.queue | None:
                caller.queue_call(handler, job)
            case RunMode.task:
                caller.call_soon(handler, job)
            case RunMode.thread:
                caller.to_thread(handler, job)
            case _ as options:
                caller.get(**options).call_soon(handler, job)

        self.log.debug("***handle message %s*** %s %s %s", msg_type, run_mode, handler, job)

    def get_shell(self, subshell_id: str | None | NoValue = NoValue) -> T_shell_co:  # pyright: ignore[reportInvalidTypeForm]
        """
        Get a shell by `subshell_id`.

        Args:
            subshell_id: The id of an existing subshell.
        """
        if (subshell_id := subshell_id if subshell_id is not NoValue else ShellPendingManager.active_id()) is None:
            return self.main_shell
        return self._subshells.get(subshell_id) or self.main_shell

    def create_subshell(self, *, protected: bool = False) -> T_shell_co:
        """
        Create a subshell.

        Use [`shell.stop(force=True)`][async_kernel.shell.base.BaseShell.stop] to stop a
        protected subshell when it is no longer required.

        Args:
            protected: Protect the subshell from accidental deletion.
        Tip:
            - `await shell.ready` to ensure the shell is 'ready'.
        """

        return self._shell_class(protected=protected)

    async def kernel_info_request(self, job: Job[Content], /) -> Content:
        """Handle an [kernel info request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-info)."""
        return self.kernel_info

    async def comm_info_request(self, job: Job[Content], /) -> Content:
        """Handle an [comm info request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#comm-info)."""
        c = job["msg"]["content"]
        target_name = c.get("target_name", None)
        comms = {
            k: {"target_name": v.target_name}
            for (k, v) in self.comm_manager.comms.copy().items()
            if v.target_name == target_name or target_name is None
        }
        return {"comms": comms}

    async def execute_request(self, job: Job[ExecuteContent], /) -> Content:
        """Handle an [execute request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#execute)."""
        return await self.shell.do_execute(
            cell_id=job["msg"]["metadata"].get("cellId"),
            received_time=job["received_time"],
            tags=job["msg"]["metadata"].get("tags", ()),
            **job["msg"]["content"],  # pyright: ignore[reportArgumentType]
        )

    async def complete_request(self, job: Job[Content], /) -> Content:
        """Handle an [completion request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#completion)."""
        return await self.shell.do_complete(
            code=job["msg"]["content"].get("code", ""), cursor_pos=job["msg"]["content"].get("cursor_pos", 0)
        )

    async def is_complete_request(self, job: Job[Content], /) -> Content:
        """Handle an [is_complete request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#code-completeness)."""
        return await self.shell.is_complete(job["msg"]["content"].get("code", ""))

    async def inspect_request(self, job: Job[Content], /) -> Content:
        """Handle an [inspect request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#introspection)."""
        c = job["msg"]["content"]
        return await self.shell.do_inspect(
            code=c.get("code", ""),
            cursor_pos=c.get("cursor_pos", 0),
            detail_level=c.get("detail_level", 0),
        )

    async def history_request(self, job: Job[Content], /) -> Content:
        """Handle an [history request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#history)."""
        return await self.shell.do_history(**job["msg"]["content"])

    async def comm_open(self, job: Job[Content], /) -> None:
        """Handle an [comm open request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#opening-a-comm)."""
        self.comm_manager.comm_open(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

    async def comm_msg(self, job: Job[Content], /) -> None:
        """Handle an [comm msg request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#comm-messages)."""
        self.comm_manager.comm_msg(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

    async def comm_close(self, job: Job[Content], /) -> None:
        """Handle an [comm close request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#tearing-down-comms)."""
        self.comm_manager.comm_close(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

    async def interrupt_request(self, job: Job[Content], /) -> Content:
        """Handle an [interrupt request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-interrupt)."""
        await self.do_interrupt()
        return {}

    async def shutdown_request(self, job: Job[Content], /) -> Content:
        """Handle an [shutdown request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-shutdown)."""
        self._restart = job["msg"]["content"].get("restart", False)
        self.parent.stop()
        return {"restart": self._restart}

    async def debug_request(self, job: Job[Content], /) -> Content:
        """Handle an [debug request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#debug-request)."""
        return await self.debugger.process_request(job["msg"]["content"])

    async def create_subshell_request(self: Kernel, job: Job[Content], /) -> Content:
        """Handle an [create subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#create-subshell)."""
        async with self.caller.create_pending_group():
            shell = self.create_subshell(protected=False)
            return {"subshell_id": shell.subshell_id}

    async def delete_subshell_request(self, job: Job[Content], /) -> Content:
        """Handle an [delete subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#delete-subshell)."""
        if (subshell_id := job["msg"]["content"]["subshell_id"]) and (subshell := self._subshells.get(subshell_id)):
            subshell.stop()
        return {}

    async def list_subshell_request(self, job: Job[Content], /) -> Content:
        """Handle an [list subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#list-subshells)."""
        return {"subshell_id": list(self._subshells)}

    def get_parent(self) -> Message[dict[str, Any]] | None:
        """
        A convenience method to access the 'message' in the current context if there is one.

        'parent' is the parameter name used by [Session.send][jupyter_client.session.Session.send] to provide context when sending a reply.

        See also:
            - [ipywidgets.Output][ipywidgets.widgets.widget_output.Output]:
                Uses `get_ipython().kernel.get_parent()` to obtain the `msg_id` which
                is used to 'capture' output when its context has been acquired.
        """
        return utils.get_parent_message()

    async def do_complete(self, code: str, cursor_pos: int | None) -> Content:
        "Matches signature of [ipykernel.kernelbase.Kernel.do_complete][]."
        return await self.shell.do_complete(code=code, cursor_pos=cursor_pos)

    async def do_inspect(
        self, code: str, cursor_pos: int = 0, detail_level: Literal[0, 1] = 0, omit_sections=()
    ) -> Content:
        "Matches signature of [ipykernel.kernelbase.Kernel.do_inspect][]."
        return await self.shell.do_inspect(code=code, cursor_pos=cursor_pos, detail_level=detail_level)

    async def do_history(
        self,
        hist_access_type,
        output,
        raw,
        session=None,
        start=None,
        stop=None,
        n=None,
        pattern=None,
        unique=False,
    ) -> Content:
        "Matches signature of [ipykernel.kernelbase.Kernel.do_history][]."
        return await self.shell.do_history(
            output=output,
            raw=raw,
            hist_access_type=hist_access_type,
            session=session,  # pyright: ignore[reportArgumentType]
            start=start,  # pyright: ignore[reportArgumentType]
            stop=stop,
        )

    async def do_execute(
        self,
        code: str,
        silent: bool = True,
        store_history: bool = True,
        user_expressions: dict[str, str] | None = None,
        allow_stdin: bool = False,
        *,
        cell_meta: dict[str, Any] | None = None,
        cell_id: str | None = None,
        **_ignored,
    ) -> Content:
        "Matches signature of [ipykernel.kernelbase.Kernel.do_execute][]."
        return await self.shell.do_execute(
            code=code,
            silent=silent,
            store_history=store_history,
            user_expressions=user_expressions,
            allow_stdin=allow_stdin,
            cell_id=cell_id,
        )

    def getpass(self, prompt="", stream=None) -> str:
        "Matches signature of [ipykernel.kernelbase.Kernel.getpass][]."
        return self.parent.input_request(str(prompt), password=True)

    def raw_input(self, prompt="") -> str:
        "Matches signature of [ipykernel.kernelbase.Kernel.raw_input][]."
        return self.parent.input_request(str(prompt), password=False)
help_links = traitlets.List(trait=traitlets.Dict()).tag(config=True)

A list of links provided kernel info request.

supported_features class-attribute instance-attribute

supported_features = traitlets.List(traitlets.Unicode()).tag(config=True)

A list of features supported by the kernel.

handle_in_shell_thread class-attribute instance-attribute

handle_in_shell_thread = traitlets.List(
    traitlets.UseEnum(MsgType), [MsgType.comm_msg, MsgType.comm_open, MsgType.comm_close]
).tag(config=True)

A list of MsgType that are always handled in the shell's thread (typically the MainThread).

handle_in_thread class-attribute instance-attribute

handle_in_thread = traitlets.Dict(
    key_trait=traitlets.UseEnum(MsgType), value_trait=traitlets.Unicode()
)

A mapping of MsgType to the name of a separate caller (thread) in which to run the handler.

callers class-attribute instance-attribute

callers: Fixed[Self, dict] = Fixed(lambda c: c['owner'].parent.callers)

A shortcut to the callers dict on the parent.

caller class-attribute instance-attribute

caller: Fixed[Self, Caller] = Fixed(lambda c: c['owner'].callers[Channel.shell])

The caller for the shell thread.

debugger class-attribute instance-attribute

debugger = Fixed(Debugger)

The debugger for handling debug requests.

comm_manager class-attribute instance-attribute

comm_manager = Fixed(CommManager)

Creates async_kernel.comm.Comm instances and maintains a mapping to comm_id to Comm instances.

active_execute_requests class-attribute instance-attribute

active_execute_requests: Fixed[Self, set[Pending[Any]]] = Fixed(set)

A set of active execute requests that gets updated by the shell.

kernel_info property

kernel_info: dict[str, Any]

Info provided to a kernel info request.

shell property

shell: T_shell_co

The shell given the current context.

Notes
  • The subshell_id of the main shell is None.

running async

running() -> AsyncGenerator[None]

The kernel runs in this context.

Notes
  • Entered by the interface (parent).
  • Overload as required.
Source code in src/async_kernel/kernel.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
@asynccontextmanager
async def running(self) -> AsyncGenerator[None]:
    """
    The kernel runs in this context.

    Notes:
        - Entered by the interface (parent).
        - Overload as required.
    """
    remove_patch = self.apply_patches()
    try:
        self._main_shell = self._shell_class(protected=True, is_mainshell=True)
        async with self._main_shell.mainshell_running():
            self.log.info("Kernel started")
            yield
    finally:
        self.log.info("Kernel stopped")
        for subshell in self._subshells.copy().values():
            subshell.stop(force=True)
        for comm in self.comm_manager.comms.copy().values():
            comm.close(deleting=True)
        remove_patch()
        self._handler_cache.clear()

do_interrupt async

do_interrupt() -> None

Interrupt/cancel non-silent active execute requests.

Source code in src/async_kernel/kernel.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
async def do_interrupt(self) -> None:
    """
    Interrupt/cancel non-silent active execute requests.
    """
    assert Caller() is self.callers[Channel.control], "Must be called from the control thread."
    for pen in self.active_execute_requests.copy():
        if not pen.metadata.get("kwargs", {}).get("silent", False):
            pen.cancel(self._interrupt_message)
    if (
        (sys.platform != "emscripten")
        and (not self.debugger.enabled or not self.debugger.stopped_threads)
        and (caller := Caller("MainThread")).running  # Can only interrupt the main thread
    ):
        self._interrupt_requested = pen = Pending()
        caller.call_direct(lambda: pen.set_result(None))
        try:
            await pen.wait(result=False, timeout=1, protect=True)
        except TimeoutError:
            if sys.platform == "win32":
                signal.raise_signal(signal.SIGINT)
            else:
                os.kill(os.getpid(), signal.SIGINT)
            await pen.wait(result=False, timeout=10, protect=True)

apply_patches

apply_patches() -> Callable[[], None]

Apply patches returning a callable to reverse the patches.

Source code in src/async_kernel/kernel.py
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def apply_patches(self) -> Callable[[], None]:
    """Apply patches returning a callable to reverse the patches."""

    original = sys.displayhook, builtins.input, getpass.getpass
    builtins.input, sys.displayhook, getpass.getpass = self.raw_input, self.displayhook, self.getpass
    restore_comm = self.comm_manager.patch_comm()
    restore_stdout = OutStream("stdout").patch()
    restore_stderr = OutStream("stderr").patch()
    restore_signal = self._patch_signal()

    def restore() -> None:
        sys.displayhook, builtins.input, getpass.getpass = original
        restore_stdout()
        restore_stderr()
        restore_comm()
        restore_signal()

    return restore

displayhook

displayhook(result: Any)

The global patch for sys.displayhook responsible for python display callbacks.

Source code in src/async_kernel/kernel.py
343
344
345
def displayhook(self, result: Any):
    "The global patch for [sys.displayhook][] responsible for python display callbacks."
    self.get_shell().displayhook(result)

message_handler

message_handler(
    job: Job,
    send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
    iopub_send: Callable,
) -> None

Schedule handling of the job (msg) with a handler running in a Task managed by a Caller.

Each msg_type runs in a separate task, possibly in a separate thread and event loop. Typically, jobs are queued for execution by either the 'shell' or 'control' caller using queue_call.

'execute_request' messages can also specify alternate run modes: - task: Run the execute request as a task. - thread: Run the execute request in a worker thread.

The alternate run mode can be specified in a few ways:
- as a comment on the first line of the code block `# task` or `# thread`.
- As a tag `thread` or `task`

Parameters:

  • job

    (Job) –

    A dict with the msg and supporting details.

  • send_reply

    (Callable[[Job, dict], CoroutineType[Any, Any, None]]) –

    The function for the handler to use to send the reply to the message.

Source code in src/async_kernel/kernel.py
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
def message_handler(
    self,
    job: Job,
    send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
    iopub_send: Callable,
    /,
) -> None:
    """
    Schedule handling of the job (msg) with a handler running in a Task managed by a Caller.

    Each `msg_type` runs in a separate task, possibly in a separate thread and event loop.
    Typically, jobs are queued for execution by either the 'shell' or 'control' caller using
    [queue_call][async_kernel.caller.Caller.queue_call].

    'execute_request' messages can also specify alternate run modes:
        - task: Run the execute request as a task.
        - thread: Run the execute request in a worker thread.

        The alternate run mode can be specified in a few ways:
        - as a comment on the first line of the code block `# task` or `# thread`.
        - As a tag `thread` or `task`

    Args:
        job: A dict with the msg and supporting details.
        send_reply: The function for the handler to use to send the reply to the message.
    """
    handler = self._get_handler(job, send_reply, iopub_send)

    run_mode: RunMode | CallerCreateOptions | None = None
    msg_type = MsgType(job["msg"]["header"]["msg_type"])

    if msg_type is MsgType.execute_request:
        caller = self.callers[job["msg"]["channel"]]
        try:
            run_mode = next(mode for tag in utils.get_tags(job) if (mode := RunMode.to_runmode(tag)))
        except StopIteration:
            if content := job["msg"].get("content", {}):
                if (code := content.get("code")) and (
                    mode := RunMode.to_runmode(code.strip().split("\n", maxsplit=1)[0])
                ):
                    run_mode = mode
                if content.get("silent"):
                    run_mode = RunMode.task

    elif msg_type in self.handle_in_shell_thread:
        caller = self.callers[Channel.shell]
    else:
        caller = self.callers[Channel.control]
        if thread_name := self.handle_in_thread.get(msg_type):
            caller = caller.get(name=thread_name, no_debug=True)

    match run_mode:
        case RunMode.queue | None:
            caller.queue_call(handler, job)
        case RunMode.task:
            caller.call_soon(handler, job)
        case RunMode.thread:
            caller.to_thread(handler, job)
        case _ as options:
            caller.get(**options).call_soon(handler, job)

    self.log.debug("***handle message %s*** %s %s %s", msg_type, run_mode, handler, job)

get_shell

get_shell(subshell_id: str | None | NoValue = NoValue) -> T_shell_co

Get a shell by subshell_id.

Parameters:

Source code in src/async_kernel/kernel.py
478
479
480
481
482
483
484
485
486
487
def get_shell(self, subshell_id: str | None | NoValue = NoValue) -> T_shell_co:  # pyright: ignore[reportInvalidTypeForm]
    """
    Get a shell by `subshell_id`.

    Args:
        subshell_id: The id of an existing subshell.
    """
    if (subshell_id := subshell_id if subshell_id is not NoValue else ShellPendingManager.active_id()) is None:
        return self.main_shell
    return self._subshells.get(subshell_id) or self.main_shell

create_subshell

create_subshell(*, protected: bool = False) -> T_shell_co

Create a subshell.

Use shell.stop(force=True) to stop a protected subshell when it is no longer required.

Parameters:

  • protected

    (bool, default: False ) –

    Protect the subshell from accidental deletion.

Tip: - await shell.ready to ensure the shell is 'ready'.

Source code in src/async_kernel/kernel.py
489
490
491
492
493
494
495
496
497
498
499
500
501
502
def create_subshell(self, *, protected: bool = False) -> T_shell_co:
    """
    Create a subshell.

    Use [`shell.stop(force=True)`][async_kernel.shell.base.BaseShell.stop] to stop a
    protected subshell when it is no longer required.

    Args:
        protected: Protect the subshell from accidental deletion.
    Tip:
        - `await shell.ready` to ensure the shell is 'ready'.
    """

    return self._shell_class(protected=protected)

kernel_info_request async

kernel_info_request(job: Job[Content]) -> Content

Handle an kernel info request.

Source code in src/async_kernel/kernel.py
504
505
506
async def kernel_info_request(self, job: Job[Content], /) -> Content:
    """Handle an [kernel info request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-info)."""
    return self.kernel_info

comm_info_request async

comm_info_request(job: Job[Content]) -> Content

Handle an comm info request.

Source code in src/async_kernel/kernel.py
508
509
510
511
512
513
514
515
516
517
async def comm_info_request(self, job: Job[Content], /) -> Content:
    """Handle an [comm info request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#comm-info)."""
    c = job["msg"]["content"]
    target_name = c.get("target_name", None)
    comms = {
        k: {"target_name": v.target_name}
        for (k, v) in self.comm_manager.comms.copy().items()
        if v.target_name == target_name or target_name is None
    }
    return {"comms": comms}

execute_request async

execute_request(job: Job[ExecuteContent]) -> Content

Handle an execute request.

Source code in src/async_kernel/kernel.py
519
520
521
522
523
524
525
526
async def execute_request(self, job: Job[ExecuteContent], /) -> Content:
    """Handle an [execute request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#execute)."""
    return await self.shell.do_execute(
        cell_id=job["msg"]["metadata"].get("cellId"),
        received_time=job["received_time"],
        tags=job["msg"]["metadata"].get("tags", ()),
        **job["msg"]["content"],  # pyright: ignore[reportArgumentType]
    )

complete_request async

complete_request(job: Job[Content]) -> Content

Handle an completion request.

Source code in src/async_kernel/kernel.py
528
529
530
531
532
async def complete_request(self, job: Job[Content], /) -> Content:
    """Handle an [completion request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#completion)."""
    return await self.shell.do_complete(
        code=job["msg"]["content"].get("code", ""), cursor_pos=job["msg"]["content"].get("cursor_pos", 0)
    )

is_complete_request async

is_complete_request(job: Job[Content]) -> Content

Handle an is_complete request.

Source code in src/async_kernel/kernel.py
534
535
536
async def is_complete_request(self, job: Job[Content], /) -> Content:
    """Handle an [is_complete request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#code-completeness)."""
    return await self.shell.is_complete(job["msg"]["content"].get("code", ""))

inspect_request async

inspect_request(job: Job[Content]) -> Content

Handle an inspect request.

Source code in src/async_kernel/kernel.py
538
539
540
541
542
543
544
545
async def inspect_request(self, job: Job[Content], /) -> Content:
    """Handle an [inspect request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#introspection)."""
    c = job["msg"]["content"]
    return await self.shell.do_inspect(
        code=c.get("code", ""),
        cursor_pos=c.get("cursor_pos", 0),
        detail_level=c.get("detail_level", 0),
    )

history_request async

history_request(job: Job[Content]) -> Content

Handle an history request.

Source code in src/async_kernel/kernel.py
547
548
549
async def history_request(self, job: Job[Content], /) -> Content:
    """Handle an [history request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#history)."""
    return await self.shell.do_history(**job["msg"]["content"])

comm_open async

comm_open(job: Job[Content]) -> None

Handle an comm open request.

Source code in src/async_kernel/kernel.py
551
552
553
async def comm_open(self, job: Job[Content], /) -> None:
    """Handle an [comm open request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#opening-a-comm)."""
    self.comm_manager.comm_open(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

comm_msg async

comm_msg(job: Job[Content]) -> None

Handle an comm msg request.

Source code in src/async_kernel/kernel.py
555
556
557
async def comm_msg(self, job: Job[Content], /) -> None:
    """Handle an [comm msg request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#comm-messages)."""
    self.comm_manager.comm_msg(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

comm_close async

comm_close(job: Job[Content]) -> None

Handle an comm close request.

Source code in src/async_kernel/kernel.py
559
560
561
async def comm_close(self, job: Job[Content], /) -> None:
    """Handle an [comm close request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#tearing-down-comms)."""
    self.comm_manager.comm_close(stream=None, ident=None, msg=job["msg"])  # pyright: ignore[reportArgumentType]

interrupt_request async

interrupt_request(job: Job[Content]) -> Content

Handle an interrupt request.

Source code in src/async_kernel/kernel.py
563
564
565
566
async def interrupt_request(self, job: Job[Content], /) -> Content:
    """Handle an [interrupt request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-interrupt)."""
    await self.do_interrupt()
    return {}

shutdown_request async

shutdown_request(job: Job[Content]) -> Content

Handle an shutdown request.

Source code in src/async_kernel/kernel.py
568
569
570
571
572
async def shutdown_request(self, job: Job[Content], /) -> Content:
    """Handle an [shutdown request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#kernel-shutdown)."""
    self._restart = job["msg"]["content"].get("restart", False)
    self.parent.stop()
    return {"restart": self._restart}

debug_request async

debug_request(job: Job[Content]) -> Content

Handle an debug request.

Source code in src/async_kernel/kernel.py
574
575
576
async def debug_request(self, job: Job[Content], /) -> Content:
    """Handle an [debug request](https://jupyter-client.readthedocs.io/en/stable/messaging.html#debug-request)."""
    return await self.debugger.process_request(job["msg"]["content"])

create_subshell_request async

create_subshell_request(job: Job[Content]) -> Content

Handle an create subshell request.

Source code in src/async_kernel/kernel.py
578
579
580
581
582
async def create_subshell_request(self: Kernel, job: Job[Content], /) -> Content:
    """Handle an [create subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#create-subshell)."""
    async with self.caller.create_pending_group():
        shell = self.create_subshell(protected=False)
        return {"subshell_id": shell.subshell_id}

delete_subshell_request async

delete_subshell_request(job: Job[Content]) -> Content

Handle an delete subshell request.

Source code in src/async_kernel/kernel.py
584
585
586
587
588
async def delete_subshell_request(self, job: Job[Content], /) -> Content:
    """Handle an [delete subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#delete-subshell)."""
    if (subshell_id := job["msg"]["content"]["subshell_id"]) and (subshell := self._subshells.get(subshell_id)):
        subshell.stop()
    return {}

list_subshell_request async

list_subshell_request(job: Job[Content]) -> Content

Handle an list subshell request.

Source code in src/async_kernel/kernel.py
590
591
592
async def list_subshell_request(self, job: Job[Content], /) -> Content:
    """Handle an [list subshell request](https://jupyter.org/enhancement-proposals/91-kernel-subshells/kernel-subshells.html#list-subshells)."""
    return {"subshell_id": list(self._subshells)}

get_parent

get_parent() -> Message[dict[str, Any]] | None

A convenience method to access the 'message' in the current context if there is one.

'parent' is the parameter name used by Session.send to provide context when sending a reply.

See also
  • ipywidgets.Output: Uses get_ipython().kernel.get_parent() to obtain the msg_id which is used to 'capture' output when its context has been acquired.
Source code in src/async_kernel/kernel.py
594
595
596
597
598
599
600
601
602
603
604
605
def get_parent(self) -> Message[dict[str, Any]] | None:
    """
    A convenience method to access the 'message' in the current context if there is one.

    'parent' is the parameter name used by [Session.send][jupyter_client.session.Session.send] to provide context when sending a reply.

    See also:
        - [ipywidgets.Output][ipywidgets.widgets.widget_output.Output]:
            Uses `get_ipython().kernel.get_parent()` to obtain the `msg_id` which
            is used to 'capture' output when its context has been acquired.
    """
    return utils.get_parent_message()

do_complete async

do_complete(code: str, cursor_pos: int | None) -> Content

Matches signature of ipykernel.kernelbase.Kernel.do_complete.

Source code in src/async_kernel/kernel.py
607
608
609
async def do_complete(self, code: str, cursor_pos: int | None) -> Content:
    "Matches signature of [ipykernel.kernelbase.Kernel.do_complete][]."
    return await self.shell.do_complete(code=code, cursor_pos=cursor_pos)

do_inspect async

do_inspect(
    code: str, cursor_pos: int = 0, detail_level: Literal[0, 1] = 0, omit_sections=()
) -> Content

Matches signature of ipykernel.kernelbase.Kernel.do_inspect.

Source code in src/async_kernel/kernel.py
611
612
613
614
615
async def do_inspect(
    self, code: str, cursor_pos: int = 0, detail_level: Literal[0, 1] = 0, omit_sections=()
) -> Content:
    "Matches signature of [ipykernel.kernelbase.Kernel.do_inspect][]."
    return await self.shell.do_inspect(code=code, cursor_pos=cursor_pos, detail_level=detail_level)

do_history async

do_history(
    hist_access_type,
    output,
    raw,
    session=None,
    start=None,
    stop=None,
    n=None,
    pattern=None,
    unique=False,
) -> Content

Matches signature of ipykernel.kernelbase.Kernel.do_history.

Source code in src/async_kernel/kernel.py
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
async def do_history(
    self,
    hist_access_type,
    output,
    raw,
    session=None,
    start=None,
    stop=None,
    n=None,
    pattern=None,
    unique=False,
) -> Content:
    "Matches signature of [ipykernel.kernelbase.Kernel.do_history][]."
    return await self.shell.do_history(
        output=output,
        raw=raw,
        hist_access_type=hist_access_type,
        session=session,  # pyright: ignore[reportArgumentType]
        start=start,  # pyright: ignore[reportArgumentType]
        stop=stop,
    )

do_execute async

do_execute(
    code: str,
    silent: bool = True,
    store_history: bool = True,
    user_expressions: dict[str, str] | None = None,
    allow_stdin: bool = False,
    *,
    cell_meta: dict[str, Any] | None = None,
    cell_id: str | None = None,
    **_ignored,
) -> Content

Matches signature of ipykernel.kernelbase.Kernel.do_execute.

Source code in src/async_kernel/kernel.py
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
async def do_execute(
    self,
    code: str,
    silent: bool = True,
    store_history: bool = True,
    user_expressions: dict[str, str] | None = None,
    allow_stdin: bool = False,
    *,
    cell_meta: dict[str, Any] | None = None,
    cell_id: str | None = None,
    **_ignored,
) -> Content:
    "Matches signature of [ipykernel.kernelbase.Kernel.do_execute][]."
    return await self.shell.do_execute(
        code=code,
        silent=silent,
        store_history=store_history,
        user_expressions=user_expressions,
        allow_stdin=allow_stdin,
        cell_id=cell_id,
    )

getpass

getpass(prompt='', stream=None) -> str

Matches signature of ipykernel.kernelbase.Kernel.getpass.

Source code in src/async_kernel/kernel.py
661
662
663
def getpass(self, prompt="", stream=None) -> str:
    "Matches signature of [ipykernel.kernelbase.Kernel.getpass][]."
    return self.parent.input_request(str(prompt), password=True)

raw_input

raw_input(prompt='') -> str

Matches signature of ipykernel.kernelbase.Kernel.raw_input.

Source code in src/async_kernel/kernel.py
665
666
667
def raw_input(self, prompt="") -> str:
    "Matches signature of [ipykernel.kernelbase.Kernel.raw_input][]."
    return self.parent.input_request(str(prompt), password=False)