kernel
Classes:
-
KernelInterruptError–Raised to interrupt the kernel.
-
Kernel–A Jupyter kernel that supports concurrent execution providing an IPython InteractiveShell
KernelInterruptError
¶
Bases: Exception
Raised to interrupt the kernel.
Source code in src/async_kernel/asyncshell.py
44 45 | |
Kernel
¶
Bases: HasTraits, AsyncContextManagerMixin
A Jupyter kernel that supports concurrent execution providing an IPython InteractiveShell with support for kernel subshells.
Info
Only one instance of a kernel is created at a time per subprocess. The instance can be obtained
with Kernel() or [get_kernel].
Starting the kernel
The kernel should appear in the list of kernels just as other kernels are. Variants of the kernel can with custom configuration can be added at the command line.
async-kernel -f .
import async_kernel.interface
async_kernel.interface.start_kernel_zmq_interface()
async with Kernel():
await anyio.sleep_forever()
Warning
Starting the kernel outside the main thread has the following implicatations: - Execute requests won't be run in the main thread. - Interrupts via signals won't work, so thread blocking calls in the shell cannot be interrupted.
Methods:
-
load_settings–Load settings into the kernel.
-
load_connection_info–Load connection info from a dict containing connection info.
-
stop–A staticmethod to stop the running kernel.
-
__asynccontextmanager__–Start the kernel in an already running anyio event loop.
-
iopub_send–Send a message on the iopub socket.
-
topic–prefixed topic for IOPub messages.
-
msg_handler–Schedule a message to be executed.
-
run_handler–Asynchronously run a message handler for a given job, managing reply sending and execution state.
-
get_run_mode–Determine the run mode for a given channel, message type and job.
-
all_concurrency_run_modes–Generates a dictionary containing all combinations of SocketID, and MsgType, along with their
-
kernel_info_request–Handle a kernel info request.
-
comm_info_request–Handle a comm info request.
-
execute_request–Handle a execute request.
-
complete_request–Handle a completion request.
-
is_complete_request–Handle a is_complete request.
-
inspect_request–Handle a inspect request.
-
history_request–Handle a history request.
-
comm_open–Handle a comm open request.
-
comm_msg–Handle a comm msg request.
-
comm_close–Handle a comm close request.
-
interrupt_request–Handle an interrupt request (control only).
-
shutdown_request–Handle a shutdown request (control only).
-
debug_request–Handle a debug request (control only).
-
create_subshell_request–Handle a create subshell request (control only).
-
delete_subshell_request–Handle a delete subshell request (control only).
-
list_subshell_request–Handle a list subshell request (control only).
-
excepthook–Handle an exception.
-
unraisablehook–Handle unraisable exceptions (during gc for instance).
-
get_connection_info–Return the connection info as a dict.
-
get_parent–A convenience method to access the 'message' in the current context if there is one.
Attributes:
-
interface–The abstraction to communicate with the kernel.
-
callers(Fixed[Self, dict[Literal[shell, control], Caller]]) –The callers associated with the kernel once it has started.
-
subshell_manager–Dedicated to management of sub shells.
-
help_links– -
quiet–Only send stdout/stderr to output stream.
-
print_kernel_messages–When enabled the kernel will print startup, shutdown and terminal errors.
-
connection_file(TraitType[Path, Path | str]) –JSON file in which to store connection info
-
kernel_name–The kernels name - if it contains 'trio' a trio backend will be used instead of an asyncio backend.
-
log–The logging adapter.
-
main_shell–The interactive shell.
-
debugger–Handles debug requests.
-
comm_manager–Creates async_kernel.comm.Comm instances and maintains a mapping to
comm_idtoComminstances. -
event_started–An event that occurs when the kernel is started.
-
event_stopped–An event that occurs when the kernel is stopped.
-
settings(dict[str, Any]) –Settings that have been set to customise the behaviour of the kernel.
-
shell(AsyncInteractiveShell | AsyncInteractiveSubshell) –The shell given the current context.
-
caller(Caller) –The caller for the shell channel.
-
kernel_info(dict[str, str | dict[str, str | dict[str, str | int]] | Any | tuple[Any, ...] | bool]) –A dict of detail sent in reply to for a 'kernel_info_request'.
Source code in src/async_kernel/kernel.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
interface
class-attribute
instance-attribute
¶
interface = Instance(BaseKernelInterface)
The abstraction to communicate with the kernel.
callers
class-attribute
instance-attribute
¶
The callers associated with the kernel once it has started.
subshell_manager
class-attribute
instance-attribute
¶
subshell_manager = Fixed(SubshellManager)
Dedicated to management of sub shells.
quiet
class-attribute
instance-attribute
¶
quiet = Bool(True)
Only send stdout/stderr to output stream.
print_kernel_messages
class-attribute
instance-attribute
¶
print_kernel_messages = Bool(True)
When enabled the kernel will print startup, shutdown and terminal errors.
connection_file
class-attribute
instance-attribute
¶
JSON file in which to store connection info
"kernel-<pid>.json"
This file will contain the IP, ports, and authentication key needed to connect clients to this kernel. By default, this file will be created in the security dir of the current profile, but can be specified by absolute path.
kernel_name
class-attribute
instance-attribute
¶
kernel_name = CUnicode()
The kernels name - if it contains 'trio' a trio backend will be used instead of an asyncio backend.
main_shell
class-attribute
instance-attribute
¶
main_shell = Fixed(lambda _: instance())
The interactive shell.
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.
event_started
class-attribute
instance-attribute
¶
An event that occurs when the kernel is started.
event_stopped
class-attribute
instance-attribute
¶
An event that occurs when the kernel is stopped.
settings
property
¶
Settings that have been set to customise the behaviour of the kernel.
shell
property
¶
The shell given the current context.
Notes
- The
subshell_idof the main shell isNone.
kernel_info
property
¶
kernel_info: dict[
str, str | dict[str, str | dict[str, str | int]] | Any | tuple[Any, ...] | bool
]
A dict of detail sent in reply to for a 'kernel_info_request'.
load_settings
¶
Load settings into the kernel.
Permitted until the kernel async context has been entered.
Parameters:
Source code in src/async_kernel/kernel.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
load_connection_info
¶
Load connection info from a dict containing connection info.
Typically this data comes from a connection file and is called by load_connection_file.
Parameters:
-
(info¶dict[str, Any]) –Dictionary containing connection_info. See the connection_file spec for details.
Source code in src/async_kernel/kernel.py
296 297 298 299 300 301 302 303 304 305 306 | |
stop
staticmethod
¶
stop() -> None
A staticmethod to stop the running kernel.
Once an instance of a kernel is stopped the instance cannot be restarted. Instead a new instance should be started.
Source code in src/async_kernel/kernel.py
308 309 310 311 312 313 314 315 316 317 | |
__asynccontextmanager__
async
¶
__asynccontextmanager__() -> AsyncGenerator[Self]
Start the kernel in an already running anyio event loop.
Source code in src/async_kernel/kernel.py
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 | |
iopub_send
¶
iopub_send(
msg_or_type: Message[dict[str, Any]] | dict[str, Any] | str,
*,
content: Content | None = None,
metadata: dict[str, Any] | None = None,
parent: Message[dict[str, Any]] | dict[str, Any] | None | NoValue = NoValue,
ident: bytes | list[bytes] | None = None,
buffers: list[bytes] | None = None,
) -> None
Send a message on the iopub socket.
Source code in src/async_kernel/kernel.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
topic
¶
topic(topic) -> bytes
prefixed topic for IOPub messages.
Source code in src/async_kernel/kernel.py
374 375 376 | |
msg_handler
¶
msg_handler(
channel: Literal[shell, control],
msg_type: MsgType,
job: Job,
send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
)
Schedule a message to be executed.
Source code in src/async_kernel/kernel.py
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 | |
run_handler
async
¶
run_handler(
subshell_id: str | None,
send_reply: Callable[[Job, dict], CoroutineType[Any, Any, None]],
handler: HandlerType,
job: Job[dict],
) -> None
Asynchronously run a message handler for a given job, managing reply sending and execution state.
Parameters:
-
(subshell_id¶str | None) –The id of the subshell to set the context of the handler.
-
(send_reply¶Callable[[Job, dict], CoroutineType[Any, Any, None]]) –A coroutine function responsible for sending the reply.
-
(handler¶HandlerType) –A coroutine function to handle the job / message.
- It is a method on the kernel whose name corresponds to the message type that it handles.
- The handler should return a dict to use as 'content'in a reply.
- If status is not included in the dict it gets added automatically as
{'status': 'ok'}. - If a reply is not expected the handler should return
None.
-
(job¶Job[dict]) –The job dictionary containing message, socket, and identification information.
Workflow
- Sets the current job and subshell_id context variables.
- Sends a "busy" status message on the IOPub channel.
- Awaits the handler; if the handler returns a content dict, a reply is sent using send_reply.
- On exception, sends an error reply and logs the exception.
- Resets the job and subshell_id context variables.
- Sends an "idle" status message on the IOPub channel.
Notes
- Replies are sent even if exceptions occur in the handler.
- The reply message type is derived from the original request type.
Source code in src/async_kernel/kernel.py
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 | |
get_run_mode
¶
get_run_mode(
msg_type: MsgType, *, channel: Literal[shell, control] = shell, job: Job | None = None
) -> RunMode
Determine the run mode for a given channel, message type and job.
Parameters:
-
(channel¶Literal[shell, control], default:shell) –The channel the message was received on.
-
(msg_type¶MsgType) –The type of the message.
-
(job¶Job | None, default:None) –The job associated with the message, if any.
Returns:
-
RunMode–The run mode for the message.
Source code in src/async_kernel/kernel.py
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 | |
all_concurrency_run_modes
¶
all_concurrency_run_modes(
channels: Iterable[Literal[shell, control]] = (shell, control),
msg_types: Iterable[MsgType] = MsgType,
) -> dict[
Literal["SocketID", "MsgType", "RunMode"], tuple[Channel, MsgType, RunMode | None]
]
Generates a dictionary containing all combinations of SocketID, and MsgType, along with their corresponding RunMode (if available).
Source code in src/async_kernel/kernel.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | |
kernel_info_request
async
¶
Handle a kernel info request.
Source code in src/async_kernel/kernel.py
569 570 571 | |
comm_info_request
async
¶
Handle a comm info request.
-
Reference
typing
MsgTypecomm_info_request
Source code in src/async_kernel/kernel.py
573 574 575 576 577 578 579 580 581 582 | |
execute_request
async
¶
execute_request(job: Job[ExecuteContent]) -> Content
Handle a execute request.
Source code in src/async_kernel/kernel.py
584 585 586 | |
complete_request
async
¶
Handle a completion request.
Source code in src/async_kernel/kernel.py
588 589 590 591 592 | |
is_complete_request
async
¶
Handle a is_complete request.
Source code in src/async_kernel/kernel.py
594 595 596 | |
inspect_request
async
¶
Handle a inspect request.
-
Reference
typing
MsgTypeinspect_request
Source code in src/async_kernel/kernel.py
598 599 600 601 602 603 604 605 | |
history_request
async
¶
Handle a history request.
Source code in src/async_kernel/kernel.py
607 608 609 | |
comm_open
async
¶
Handle a comm open request.
Source code in src/async_kernel/kernel.py
611 612 613 | |
comm_msg
async
¶
Handle a comm msg request.
Source code in src/async_kernel/kernel.py
615 616 617 | |
comm_close
async
¶
Handle a comm close request.
Source code in src/async_kernel/kernel.py
619 620 621 | |
interrupt_request
async
¶
Handle an interrupt request (control only).
Source code in src/async_kernel/kernel.py
623 624 625 626 | |
shutdown_request
async
¶
Handle a shutdown request (control only).
Source code in src/async_kernel/kernel.py
628 629 630 631 | |
debug_request
async
¶
Handle a debug request (control only).
Source code in src/async_kernel/kernel.py
633 634 635 | |
create_subshell_request
async
¶
Handle a create subshell request (control only).
Source code in src/async_kernel/kernel.py
637 638 639 640 | |
delete_subshell_request
async
¶
Handle a delete subshell request (control only).
Source code in src/async_kernel/kernel.py
642 643 644 645 | |
list_subshell_request
async
¶
Handle a list subshell request (control only).
Source code in src/async_kernel/kernel.py
647 648 649 | |
excepthook
¶
excepthook(etype, evalue, tb) -> None
Handle an exception.
Source code in src/async_kernel/kernel.py
651 652 653 654 655 | |
unraisablehook
¶
unraisablehook(unraisable: UnraisableHookArgs) -> None
Handle unraisable exceptions (during gc for instance).
Source code in src/async_kernel/kernel.py
657 658 659 660 661 662 663 664 | |
get_connection_info
¶
Return the connection info as a dict.
Source code in src/async_kernel/kernel.py
666 667 668 669 | |
get_parent
¶
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
- Kernel.iopub_send
- ipywidgets.Output:
Uses
get_ipython().kernel.get_parent()to obtain themsg_idwhich is used to 'capture' output when its context has been acquired.
Source code in src/async_kernel/kernel.py
671 672 673 674 675 676 677 678 679 680 681 682 683 | |