async_kernel.kernel
Classes:
- 
          KernelInterruptError–Raised to interrupt the kernel. 
- 
          Kernel–An asynchronous kernel with an anyio backend providing an IPython AsyncInteractiveShell with zmq sockets. 
    
              Bases: InterruptedError
Raised to interrupt the kernel.
Kernel(settings: dict | None = None)
              Bases: HasTraits
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:
    await anyio.sleep_forever()
Tip
This is a convenient way to start a kernel for debugging.
Origins: IPyKernel Kernel, IPyKernel IPKernelApp & IPyKernel IPythonKernel
Methods:
- 
            load_connection_info–Load connection info from a dict containing connection info. 
- 
            __aenter__–Start the kernel. 
- 
            stop–Stop the running kernel. 
- 
            handle_message_request–The main handler for all shell and control messages. 
- 
            get_run_mode–Determine the run mode for a given channel, message type and concurrency mode. 
- 
            all_concurrency_run_modes–Generates a dictionary containing all combinations of SocketID, KernelConcurrencyMode, and MsgType, 
- 
            run_handler–Runs the handler in the context of the job/message sending the reply content if it is provided. 
- 
            iopub_send–Send a message on the zmq iopub socket. 
- 
            topic–prefixed topic for IOPub messages. 
- 
            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 a interrupt request (control only). 
- 
            shutdown_request–Handle a shutdown request (control only). 
- 
            debug_request–Handle a debug request (control only). 
- 
            excepthook–Handle an exception. 
- 
            unraisablehook–Handle unraisable exceptions (during gc for instance). 
- 
            raw_input–Forward raw_input to frontends. 
- 
            getpass–Forward getpass to frontends. 
- 
            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:
- 
          anyio_backend–
- 
          anyio_backend_options(Dict[Backend, dict[str, Any] | None]) –Default options to use with anyio.run. See also: Kernel.handle_message_request
- 
          concurrency_mode–The mode to use when getting the run mode for running the handler of a message request. 
- 
          help_links–
- 
          quiet–Only send stdout/stderr to output stream 
- 
          connection_file(TraitType[Path, Path | str]) –JSON file in which to store connection info [default: kernel- .json] 
- 
          kernel_name(str | Unicode) –The kernels name - if it contains 'trio' a trio backend will be used instead of an asyncio backend. 
- 
          ip–The kernel's IP address [default localhost]. 
- 
          log–
- 
          shell–
- 
          session–
- 
          debugger–
- 
          comm_manager(Instance[CommManager]) –
- 
          event_started–An event that occurs when the kernel is started. 
- 
          event_stopped–An event that occurs when the kernel is stopped. 
- 
          execution_count(int) –The execution count in context of the current coroutine, else the current value if there isn't one in context. 
Source code in src/async_kernel/kernel.py
                    | 311 312 313 314 315 316 317 318 319 320 321 322 323 |  | 
class-attribute
      instance-attribute
  
¶
    Default options to use with anyio.run. See also: Kernel.handle_message_request
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.
class-attribute
      instance-attribute
  
¶
quiet = Bool(True)
Only send stdout/stderr to output stream
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.
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.
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!
class-attribute
      instance-attribute
  
¶
comm_manager: Instance[CommManager] = Instance('async_kernel.comm.CommManager')
class-attribute
      instance-attribute
  
¶
event_started = Instance(AsyncEvent, (), read_only=True)
An event that occurs when the kernel is started.
class-attribute
      instance-attribute
  
¶
event_stopped = Instance(AsyncEvent, (), read_only=True)
An event that occurs when the kernel is stopped.
property
  
¶
execution_count: int
The execution count in context of the current coroutine, else the current value if there isn't one in context.
    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
              | 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 |  | 
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
              | 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |  | 
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
              | 459 460 461 462 463 464 465 466 467 468 469 |  | 
async
  
¶
    The main handler for all shell and control messages.
Parameters:
Source code in src/async_kernel/kernel.py
              | 722 723 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 |  | 
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
              | 750 751 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 810 811 |  | 
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
              | 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |  | 
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
              | 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 871 872 873 874 |  | 
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
              | 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 906 907 908 909 |  | 
topic(topic) -> bytes
prefixed topic for IOPub messages.
Source code in src/async_kernel/kernel.py
              | 911 912 913 |  | 
async
  
¶
kernel_info_request(job: Job[Content]) -> Content
Handle a kernel info request.
Source code in src/async_kernel/kernel.py
              | 915 916 917 |  | 
async
  
¶
comm_info_request(job: Job[Content]) -> Content
Handle a comm info request.
Source code in src/async_kernel/kernel.py
              | 919 920 921 922 923 924 925 926 927 928 |  | 
async
  
¶
execute_request(job: Job[ExecuteContent]) -> Content
Handle a execute request.
Source code in src/async_kernel/kernel.py
              | 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 989 990 991 992 |  | 
async
  
¶
complete_request(job: Job[Content]) -> Content
Handle a completion request.
Source code in src/async_kernel/kernel.py
              | 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 |  | 
async
  
¶
is_complete_request(job: Job[Content]) -> Content
Handle a is_complete request.
Source code in src/async_kernel/kernel.py
              | 1021 1022 1023 1024 1025 1026 1027 |  | 
async
  
¶
inspect_request(job: Job[Content]) -> Content
Handle a inspect request.
Source code in src/async_kernel/kernel.py
              | 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |  | 
async
  
¶
history_request(job: Job[Content]) -> Content
Handle a history request.
Source code in src/async_kernel/kernel.py
              | 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |  | 
async
  
¶
comm_open(job: Job[Content]) -> None
Handle a comm open request.
Source code in src/async_kernel/kernel.py
              | 1068 1069 1070 |  | 
async
  
¶
comm_msg(job: Job[Content]) -> None
Handle a comm msg request.
Source code in src/async_kernel/kernel.py
              | 1072 1073 1074 |  | 
async
  
¶
comm_close(job: Job[Content]) -> None
Handle a comm close request.
Source code in src/async_kernel/kernel.py
              | 1076 1077 1078 |  | 
async
  
¶
interrupt_request(job: Job[Content]) -> Content
Handle a interrupt request (control only).
Source code in src/async_kernel/kernel.py
              | 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 |  | 
async
  
¶
shutdown_request(job: Job[Content]) -> Content
Handle a shutdown request (control only).
Source code in src/async_kernel/kernel.py
              | 1092 1093 1094 1095 |  | 
async
  
¶
debug_request(job: Job[Content]) -> Content
Handle a debug request (control only).
Source code in src/async_kernel/kernel.py
              | 1097 1098 1099 |  | 
excepthook(etype, evalue, tb) -> None
Handle an exception.
Source code in src/async_kernel/kernel.py
              | 1101 1102 1103 1104 |  | 
unraisablehook(unraisable: UnraisableHookArgs) -> None
Handle unraisable exceptions (during gc for instance).
Source code in src/async_kernel/kernel.py
              | 1106 1107 1108 1109 1110 1111 1112 1113 |  | 
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
              | 1115 1116 1117 1118 1119 1120 1121 1122 1123 |  | 
getpass(prompt='') -> Any
Forward getpass to frontends.
Source code in src/async_kernel/kernel.py
              | 1125 1126 1127 |  | 
    Return the connection info as a dict.
Source code in src/async_kernel/kernel.py
              | 1129 1130 1131 1132 |  | 
    A convenience method to access the 'message' in the current context if there is one.
'parent' is the parameter name uses in Session.send.
See also
- Kernel.iopub_send
- ipywidgets.Output:
    Uses get_ipython().kernel.get_parent()to obtain themsg_idwhich is used to 'capture' output when it's context has been acquired.
Source code in src/async_kernel/kernel.py
              | 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 |  | 
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
              | 75 76 77 78 79 80 81 82 83 84 85 86 |  | 
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
              | 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 |  |