interface
Modules:
-
base–The base class definition to interface with the kernel.
-
callable–A collection of objects to provide a kernel interface based on callbacks.
-
zmq–A collection of objects defining the kernel interface using zmq sockets.
Classes:
-
BaseKernelInterface–The base class required interface with the kernel. Must be overloaded to be useful.
Functions:
-
start_kernel_callable_interface–Start the kernel with the callback based kernel interface CallableKernelInterface.
-
start_kernel_zmq_interface–Start the kernel with the zmq socket based kernel interface ZMQKernelInterface.
BaseKernelInterface
¶
Bases: HasTraits, AsyncContextManagerMixin
The base class required interface with the kernel. Must be overloaded to be useful.
Methods:
-
__asynccontextmanager__–Create caller, and open socketes.
-
input_request–Forward an input request to the frontend.
-
raw_input–Forward a raw_input request to the client.
-
getpass–Forward getpass to the client.
-
interrupt–Interrupt execution, possible raising a async_kernel.asyncshell.KernelInterruptError.
-
msg–Return the nested message dict.
-
iopub_send–Send an iopub message.
Attributes:
-
log–The logging adapter.
-
callers(Fixed[Self, dict[Literal[shell, control], Caller]]) –The caller associated with the kernel once it has started.
-
kernel(Fixed[Self, Kernel]) –The kernel.
-
interrupts(Fixed[Self, set[Callable[[], object]]]) –A set for callables can be added to run code when a kernel interrupt is initiated (control thread).
-
last_interrupt_frame–This frame is set when an interrupt is intercepted and cleared once the interrupt has been handled.
-
wait_exit–An event that when set will leave the kernel context if the kernel was started by this interface.
Source code in src/async_kernel/interface/base.py
54 55 56 57 58 59 60 61 62 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 | |
callers
class-attribute
instance-attribute
¶
The caller associated with the kernel once it has started.
kernel
class-attribute
instance-attribute
¶
The kernel.
interrupts
class-attribute
instance-attribute
¶
A set for callables can be added to run code when a kernel interrupt is initiated (control thread).
last_interrupt_frame
class-attribute
instance-attribute
¶
last_interrupt_frame = None
This frame is set when an interrupt is intercepted and cleared once the interrupt has been handled.
wait_exit
class-attribute
instance-attribute
¶
An event that when set will leave the kernel context if the kernel was started by this interface.
__asynccontextmanager__
async
¶
__asynccontextmanager__() -> AsyncGenerator[Self]
Create caller, and open socketes.
Source code in src/async_kernel/interface/base.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
input_request
¶
Forward an input request to the frontend.
Parameters:
-
(prompt¶str) –The user prompt.
-
(password¶bool, default:False) –If the prompt should be considered as a password.
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdi
Source code in src/async_kernel/interface/base.py
135 136 137 138 139 140 141 142 143 144 145 146 | |
raw_input
¶
Forward a raw_input request to the client.
Parameters:
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdin.
Source code in src/async_kernel/interface/base.py
148 149 150 151 152 153 154 155 156 157 158 | |
getpass
¶
Forward getpass to the client.
Parameters:
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdin.
Source code in src/async_kernel/interface/base.py
160 161 162 163 164 165 166 167 168 169 170 | |
interrupt
¶
interrupt()
Interrupt execution, possible raising a async_kernel.asyncshell.KernelInterruptError.
Source code in src/async_kernel/interface/base.py
172 173 174 175 176 177 178 179 180 | |
msg
¶
msg(
msg_type: str,
*,
content: dict | None = None,
parent: Message | dict[str, Any] | None = None,
header: MsgHeader | dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
channel: Channel = shell,
) -> Message[dict[str, Any]]
Return the nested message dict.
This format is different from what is sent over the wire. The serialize/deserialize methods converts this nested message dict to the wire format, which is a list of message parts.
Source code in src/async_kernel/interface/base.py
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 | |
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: dict[str, Any] | MsgHeader | None | NoValue = NoValue,
ident: bytes | list[bytes] | None = None,
buffers: list[bytes] | None = None,
) -> None
Send an iopub message.
Source code in src/async_kernel/interface/base.py
219 220 221 222 223 224 225 226 227 228 229 230 | |
start_kernel_callable_interface
async
¶
start_kernel_callable_interface(
*,
send: Callable[[str, list | None, bool], None | str],
stopped: Callable[[], None],
settings: dict | None = None,
) -> Handlers
Start the kernel with the callback based kernel interface CallableKernelInterface.
Source code in src/async_kernel/interface/__init__.py
15 16 17 18 19 20 21 22 23 24 25 26 | |
start_kernel_zmq_interface
¶
start_kernel_zmq_interface(settings: dict | None = None) -> None
Start the kernel with the zmq socket based kernel interface ZMQKernelInterface.
Available in CPython.
Source code in src/async_kernel/interface/__init__.py
29 30 31 32 33 34 35 36 37 | |
The base class definition to interface with the kernel.
Classes:
-
BaseKernelInterface–The base class required interface with the kernel. Must be overloaded to be useful.
BaseKernelInterface
¶
Bases: HasTraits, AsyncContextManagerMixin
The base class required interface with the kernel. Must be overloaded to be useful.
Methods:
-
__asynccontextmanager__–Create caller, and open socketes.
-
input_request–Forward an input request to the frontend.
-
raw_input–Forward a raw_input request to the client.
-
getpass–Forward getpass to the client.
-
interrupt–Interrupt execution, possible raising a async_kernel.asyncshell.KernelInterruptError.
-
msg–Return the nested message dict.
-
iopub_send–Send an iopub message.
Attributes:
-
log–The logging adapter.
-
callers(Fixed[Self, dict[Literal[shell, control], Caller]]) –The caller associated with the kernel once it has started.
-
kernel(Fixed[Self, Kernel]) –The kernel.
-
interrupts(Fixed[Self, set[Callable[[], object]]]) –A set for callables can be added to run code when a kernel interrupt is initiated (control thread).
-
last_interrupt_frame–This frame is set when an interrupt is intercepted and cleared once the interrupt has been handled.
-
wait_exit–An event that when set will leave the kernel context if the kernel was started by this interface.
Source code in src/async_kernel/interface/base.py
54 55 56 57 58 59 60 61 62 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 | |
callers
class-attribute
instance-attribute
¶
The caller associated with the kernel once it has started.
kernel
class-attribute
instance-attribute
¶
The kernel.
interrupts
class-attribute
instance-attribute
¶
A set for callables can be added to run code when a kernel interrupt is initiated (control thread).
last_interrupt_frame
class-attribute
instance-attribute
¶
last_interrupt_frame = None
This frame is set when an interrupt is intercepted and cleared once the interrupt has been handled.
wait_exit
class-attribute
instance-attribute
¶
An event that when set will leave the kernel context if the kernel was started by this interface.
__asynccontextmanager__
async
¶
__asynccontextmanager__() -> AsyncGenerator[Self]
Create caller, and open socketes.
Source code in src/async_kernel/interface/base.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
input_request
¶
Forward an input request to the frontend.
Parameters:
-
(prompt¶str) –The user prompt.
-
(password¶bool, default:False) –If the prompt should be considered as a password.
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdi
Source code in src/async_kernel/interface/base.py
135 136 137 138 139 140 141 142 143 144 145 146 | |
raw_input
¶
Forward a raw_input request to the client.
Parameters:
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdin.
Source code in src/async_kernel/interface/base.py
148 149 150 151 152 153 154 155 156 157 158 | |
getpass
¶
Forward getpass to the client.
Parameters:
Raises:
-
StdinNotImplementedError–if active frontend doesn't support stdin.
Source code in src/async_kernel/interface/base.py
160 161 162 163 164 165 166 167 168 169 170 | |
interrupt
¶
interrupt()
Interrupt execution, possible raising a async_kernel.asyncshell.KernelInterruptError.
Source code in src/async_kernel/interface/base.py
172 173 174 175 176 177 178 179 180 | |
msg
¶
msg(
msg_type: str,
*,
content: dict | None = None,
parent: Message | dict[str, Any] | None = None,
header: MsgHeader | dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
channel: Channel = shell,
) -> Message[dict[str, Any]]
Return the nested message dict.
This format is different from what is sent over the wire. The serialize/deserialize methods converts this nested message dict to the wire format, which is a list of message parts.
Source code in src/async_kernel/interface/base.py
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 | |
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: dict[str, Any] | MsgHeader | None | NoValue = NoValue,
ident: bytes | list[bytes] | None = None,
buffers: list[bytes] | None = None,
) -> None
Send an iopub message.
Source code in src/async_kernel/interface/base.py
219 220 221 222 223 224 225 226 227 228 229 230 | |
A collection of objects to provide a kernel interface based on callbacks.
Classes:
-
Handlers–Handlers returned by async_kernel.interface.callable.CallableKernelInterface when it is started.
-
CallableKernelInterface–A callback based interface to interact with the kernel using serialized messages.
Handlers
¶
Bases: TypedDict
Handlers returned by async_kernel.interface.callable.CallableKernelInterface when it is started.
Attributes:
-
handle_msg(Callable[[str, list[bytes] | list[bytearray] | None]]) –Handle messages from the client.
-
stop(Callable[[], None]) –Stop the kernel.
Source code in src/async_kernel/interface/callable.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
handle_msg
instance-attribute
¶
Handle messages from the client.
The handler requires two positional arguments
- The message serialized as a JSON string. The channel ("shell" or "control" ) should also be included in the Message under the key "channel".
- A list of buffers if there are any, or None if there are no buffers.
CallableKernelInterface
¶
Bases: BaseKernelInterface
A callback based interface to interact with the kernel using serialized messages.
Usage:
```python
from async_kernel.interface.callable import CallableKernelInterface
# Start the kernel providing the necessary callbacks.
kernel_interface = await CallableKernelInterface(options).start(send=..., stopped=...)
# Pass messages to the kernel.
kernel_interface["handle_msg"](msg, buffer)
# Stop the kernel.
kernel_interface["stop"](msg, buffer)
```
See also: - [async_kernel.typing.CallableKernelInterfaceReturnArgs]
Methods:
-
pack–Pack a message to a string.
-
unpack–Unpack a message from a json string.
-
start–Start the kernel.
Source code in src/async_kernel/interface/callable.py
50 51 52 53 54 55 56 57 58 59 60 61 62 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 | |
pack
¶
Pack a message to a string.
Source code in src/async_kernel/interface/callable.py
75 76 77 78 79 | |
unpack
¶
Unpack a message from a json string.
Source code in src/async_kernel/interface/callable.py
81 82 83 84 85 86 87 88 | |
start
async
¶
start(
*, send: Callable[[str, list | None, bool], None | str], stopped: Callable[[], None]
) -> Handlers
Start the kernel.
Parameters:
-
(send¶Callable[[str, list | None, bool], None | str]) –The function to send kernel messages to the client. It must accept
- A json string of the message.
- A list of buffers, or None if there are no buffers.
- A boolean value that indicates a response is required for the stdio channel.
-
(stopped¶Callable[[], None]) –A callback that is called once the kernel has stopped.
Returns: A pending that when resolved returns the message handler callback.
Source code in src/async_kernel/interface/callable.py
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 | |
A collection of objects defining the kernel interface using zmq sockets.
Classes:
-
ZMQKernelInterface–An interface for the kernel that uses zmq sockets.
ZMQKernelInterface
¶
Bases: BaseKernelInterface
An interface for the kernel that uses zmq sockets.
- Reference interface
Methods:
-
start–Start the kernel blocking until the kernel stops.
-
load_connection_info–Load connection info from a dict containing connection info.
-
__asynccontextmanager__–Create caller, and open socketes.
-
iopub_send–Send a message on the zmq iopub socket.
-
receive_msg_loop–Opens a zmq socket for the channel, receives messages and calls the message handler.
Attributes:
-
sockets(Fixed[Self, dict[Channel, Socket]]) – -
ports(Fixed[Self, dict[Channel, int]]) – -
ip–The kernel's IP address [default localhost].
-
session–Handles serialization and sending of messages.
-
transport(CaselessStrEnum[str]) –Transport for sockets.
-
backend(Container[Backend]) –The the backend used to provide the shell event loop.
-
backend_options–The
backend_optionsto use with anyio.run.
Source code in src/async_kernel/interface/zmq.py
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 | |
sockets
class-attribute
instance-attribute
¶
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!
session
class-attribute
instance-attribute
¶
Handles serialization and sending of messages.
transport
class-attribute
instance-attribute
¶
transport: CaselessStrEnum[str] = CaselessStrEnum(
["tcp", "ipc"] if platform == "linux" else ["tcp"], default_value="tcp"
)
Transport for sockets.
backend
class-attribute
instance-attribute
¶
backend: Container[Backend] = UseEnum(Backend)
The the backend used to provide the shell event loop.
backend_options
class-attribute
instance-attribute
¶
backend_options = Dict(allow_none=True)
The backend_options to use with anyio.run.
start
¶
start()
Start the kernel blocking until the kernel stops.
Warning
- Running the kernel in a thread other than the 'MainThread' is permitted, but discouraged.
- Blocking calls can only be interrupted in the 'MainThread' because 'threads cannot be destroyed, stopped, suspended, resumed, or interrupted'.
- Some libraries may assume the call is occurring in the 'MainThread'.
- If there is an
asyncioortrioevent loop already running in the 'MainThread. Useasync with kernelinstead.
Source code in src/async_kernel/interface/zmq.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
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/interface/zmq.py
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 | |
__asynccontextmanager__
async
¶
__asynccontextmanager__() -> AsyncGenerator[Self]
Create caller, and open socketes.
Source code in src/async_kernel/interface/zmq.py
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 | |
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: dict[str, Any] | MsgHeader | 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/interface/zmq.py
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 | |
receive_msg_loop
¶
Opens a zmq socket for the channel, receives messages and calls the message handler.
Source code in src/async_kernel/interface/zmq.py
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 | |