Kernel
async_kernel.kernel ¶
Classes:
-
Kernel
–An asynchronous kernel with an anyio backend providing an IPython AsyncInteractiveShell with zmq sockets.
-
KernelInterruptError
–Raised to interrupt the kernel.
Kernel ¶
Kernel(settings: dict | None = None)
An asynchronous kernel with an anyio backend providing an IPython AsyncInteractiveShell with zmq sockets.
Only one instance will be created/run at a time. The instance can be obtained with Kernel()
or [async_kernel.utils.get_kernel].
To start the kernel:
At the command prompt.
async-kernel -f .
See also:
-
from async_kernel.__main__ import main
main()
Kernel.start()
kernel = Kernel()
async with kernel.start_in_context():
await anyio.sleep_forever()
Tip
This is a convenient way to start a kernel for debugging.
Origins: IPyKernel Kernel, IPyKernel IPKernelApp & IPyKernel IPythonKernel
Methods:
-
__aenter__
–Start the kernel.
-
all_concurrency_run_modes
–Generates a dictionary containing all combinations of SocketID, KernelConcurrencyMode, and MsgType,
-
comm_close
–Handle a comm close request.
-
comm_info_request
–Handle a comm info request.
-
comm_msg
–Handle a comm msg request.
-
comm_open
–Handle a comm open request.
-
complete_request
–Handle a completion request.
-
debug_request
–Handle a debug request (control only).
-
excepthook
–Handle an exception.
-
execute_request
–Handle a execute request.
-
get_connection_info
–Return the connection info as a dict.
-
get_run_mode
–Determine the run mode for a given channel, message type and concurrency mode.
-
getpass
–Forward getpass to frontends.
-
handle_message_request
–The main handler for all shell and control messages.
-
history_request
–Handle a history request.
-
inspect_request
–Handle a inspect request.
-
interrupt_request
–Handle a interrupt request (control only).
-
iopub_send
–Send a message on the zmq iopub socket.
-
is_complete_request
–Handle a is_complete request.
-
kernel_info_request
–Handle a kernel info request.
-
load_connection_info
–Load connection info from a dict containing connection info.
-
raw_input
–Forward raw_input to frontends.
-
run_handler
–Runs the handler in the context of the job/message sending the reply content if it is provided.
-
shutdown_request
–Handle a shutdown request (control only).
-
stop
–Stop the running kernel.
-
topic
–prefixed topic for IOPub messages.
-
unraisablehook
–Handle unraisable exceptions (during gc for instance).
Attributes:
-
anyio_backend
– -
anyio_backend_options
(Dict[Backend, dict[str, Any] | None]
) –Default options to use with anyio.run. See also:
Kernel.handle_message_request
-
comm_manager
(Instance[CommManager]
) – -
concurrency_mode
–The mode to use when getting the run mode for running the handler of a message request.
-
connection_file
(TraitType[Path, Path | str]
) –JSON file in which to store connection info [default: kernel-
.json] -
debugger
– -
execution_count
(int
) –The execution count in context of the current coroutine, else the current value if there isn't one in context.
-
help_links
– -
ip
–The kernel's IP address [default localhost].
-
kernel_name
(str | Unicode
) –The kernels name - if it contains 'trio' a trio backend will be used instead of an asyncio backend.
-
log
– -
quiet
–Only send stdout/stderr to output stream
-
session
– -
shell
–
Source code in src/async_kernel/kernel.py
306 307 308 309 310 311 312 313 314 315 316 |
|
anyio_backend_options
class-attribute
instance-attribute
¶
Default options to use with anyio.run. See also: Kernel.handle_message_request
comm_manager
class-attribute
instance-attribute
¶
comm_manager: Instance[CommManager] = Instance('async_kernel.comm.CommManager')
concurrency_mode
class-attribute
instance-attribute
¶
concurrency_mode = UseEnum(KernelConcurrencyMode)
The mode to use when getting the run mode for running the handler of a message request.
connection_file
class-attribute
instance-attribute
¶
JSON file in which to store connection info [default: kernel-
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.
execution_count
property
¶
execution_count: int
The execution count in context of the current coroutine, else the current value if there isn't one in context.
ip
class-attribute
instance-attribute
¶
ip = Unicode()
The kernel's IP address [default localhost].
If the IP address is something other than localhost, then Consoles on other machines will be able to connect to the Kernel, so be careful!
kernel_name
class-attribute
instance-attribute
¶
kernel_name: str | Unicode = Unicode()
The kernels name - if it contains 'trio' a trio backend will be used instead of an asyncio backend.
quiet
class-attribute
instance-attribute
¶
quiet = Bool(True)
Only send stdout/stderr to output stream
__aenter__
async
¶
__aenter__() -> Self
Start the kernel.
- Only one instance can (should) run at a time.
- An instance can only be started once.
- A new instance can be started after a previous instance has stopped.
Example
async with Kerne() as kernel:
await anyio.sleep_forever()
Source code in src/async_kernel/kernel.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
|
all_concurrency_run_modes ¶
all_concurrency_run_modes(
socket_ids: Iterable[Literal[shell, control]] = (shell, control),
msg_types: Iterable[MsgType] = MsgType,
) -> dict[
Literal["SocketID", "KernelConcurrencyMode", "MsgType", "RunMode"],
tuple[SocketID, KernelConcurrencyMode, MsgType, RunMode | None],
]
Generates a dictionary containing all combinations of SocketID, KernelConcurrencyMode, and MsgType, along with their corresponding RunMode (if available).
Source code in src/async_kernel/kernel.py
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
|
comm_close
async
¶
comm_close(job: Job[Content]) -> None
Handle a comm close request.
Source code in src/async_kernel/kernel.py
1072 1073 1074 |
|
comm_info_request
async
¶
comm_info_request(job: Job[Content]) -> Content
Handle a comm info request.
Source code in src/async_kernel/kernel.py
915 916 917 918 919 920 921 922 923 924 |
|
comm_msg
async
¶
comm_msg(job: Job[Content]) -> None
Handle a comm msg request.
Source code in src/async_kernel/kernel.py
1068 1069 1070 |
|
comm_open
async
¶
comm_open(job: Job[Content]) -> None
Handle a comm open request.
Source code in src/async_kernel/kernel.py
1064 1065 1066 |
|
complete_request
async
¶
complete_request(job: Job[Content]) -> Content
Handle a completion request.
Source code in src/async_kernel/kernel.py
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 |
|
debug_request
async
¶
debug_request(job: Job[Content]) -> Content
Handle a debug request (control only).
Source code in src/async_kernel/kernel.py
1094 1095 1096 |
|
excepthook ¶
excepthook(etype, evalue, tb) -> None
Handle an exception.
Source code in src/async_kernel/kernel.py
1098 1099 1100 1101 |
|
execute_request
async
¶
execute_request(job: Job[ExecuteContent]) -> Content
Handle a execute request.
Source code in src/async_kernel/kernel.py
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
|
get_connection_info ¶
Return the connection info as a dict.
Source code in src/async_kernel/kernel.py
1126 1127 1128 1129 |
|
get_run_mode ¶
get_run_mode(
msg_type: MsgType,
*,
socket_id: Literal[shell, control] = shell,
concurrency_mode: KernelConcurrencyMode | NoValue = NoValue,
job: Job | None = None,
) -> RunMode
Determine the run mode for a given channel, message type and concurrency mode.
The run mode determines how the kernel will execute the message.
Parameters:
-
socket_id
¶Literal[shell, control]
, default:shell
) –The socket ID the message was received on.
-
msg_type
¶MsgType
) –The type of the message.
-
concurrency_mode
¶KernelConcurrencyMode | NoValue
, default:NoValue
) –The concurrency mode of the kernel. Defaults to kernel.concurrency_mode
-
job
¶Job | None
, default:None
) –The job associated with the message, if any.
Returns:
-
RunMode
–The run mode for the message.
Raises:
-
ValueError
–If a shutdown or debug request is received on the shell socket.
Source code in src/async_kernel/kernel.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
|
getpass ¶
getpass(prompt='') -> Any
Forward getpass to frontends.
Source code in src/async_kernel/kernel.py
1122 1123 1124 |
|
handle_message_request
async
¶
The main handler for all shell and control messages.
Parameters:
Source code in src/async_kernel/kernel.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
history_request
async
¶
history_request(job: Job[Content]) -> Content
Handle a history request.
Source code in src/async_kernel/kernel.py
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 |
|
inspect_request
async
¶
inspect_request(job: Job[Content]) -> Content
Handle a inspect request.
Source code in src/async_kernel/kernel.py
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 |
|
interrupt_request
async
¶
interrupt_request(job: Job[Content]) -> Content
Handle a interrupt request (control only).
Source code in src/async_kernel/kernel.py
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
|
iopub_send ¶
iopub_send(
msg_or_type: dict[str, Any] | str,
content: Content | None = None,
metadata: dict[str, Any] | None = None,
parent: dict[str, Any] | None | NoValue = NoValue,
ident: bytes | list[bytes] | None = None,
buffers: list[bytes] | None = None,
) -> None
Send a message on the zmq iopub socket.
Source code in src/async_kernel/kernel.py
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 |
|
is_complete_request
async
¶
is_complete_request(job: Job[Content]) -> Content
Handle a is_complete request.
Source code in src/async_kernel/kernel.py
1017 1018 1019 1020 1021 1022 1023 |
|
kernel_info_request
async
¶
kernel_info_request(job: Job[Content]) -> Content
Handle a kernel info request.
Source code in src/async_kernel/kernel.py
911 912 913 |
|
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
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 |
|
raw_input ¶
raw_input(prompt='') -> Any
Forward raw_input to frontends.
Raises¶
StdinNotImplementedError if active frontend doesn't support stdin.
Source code in src/async_kernel/kernel.py
1112 1113 1114 1115 1116 1117 1118 1119 1120 |
|
run_handler
async
¶
Runs the handler in the context of the job/message sending the reply content if it is provided.
This method gets called for every valid request with the relevant handler.
Source code in src/async_kernel/kernel.py
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
|
shutdown_request
async
¶
shutdown_request(job: Job[Content]) -> Content
Handle a shutdown request (control only).
Source code in src/async_kernel/kernel.py
1088 1089 1090 1091 1092 |
|
stop
staticmethod
¶
stop() -> None
Stop the running kernel.
Once a kernel is stopped; that instance of the kernel cannot be restarted. Instead, a new kernel must be started.
Source code in src/async_kernel/kernel.py
452 453 454 455 456 457 458 459 460 461 462 |
|
topic ¶
topic(topic) -> bytes
prefixed topic for IOPub messages.
Source code in src/async_kernel/kernel.py
907 908 909 |
|
unraisablehook ¶
unraisablehook(unraisable: UnraisableHookArgs) -> None
Handle unraisable exceptions (during gc for instance).
Source code in src/async_kernel/kernel.py
1103 1104 1105 1106 1107 1108 1109 1110 |
|
KernelInterruptError ¶
Raised to interrupt the kernel.
bind_socket ¶
bind_socket(
socket: Socket[SocketType],
transport: Literal["tcp", "ipc"],
ip: str,
port: int = 0,
max_attempts: int | NoValue = NoValue,
) -> int
Bind the socket to a port using the settings.
max_attempts: The maximum number of attempts to bind the socket. If un-specified, defaults to 100 if port missing, else 2 attempts.
Source code in src/async_kernel/kernel.py
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 |
|
error_to_content ¶
error_to_content(error: BaseException) -> Content
Convert the error to a dict.
ref: https://jupyter-client.readthedocs.io/en/stable/messaging.html#request-reply
Source code in src/async_kernel/kernel.py
74 75 76 77 78 79 80 81 82 83 84 85 |
|