messaging
Modules:
-
base–Default Messaging object definitions for Messaging including
BaseMessage,ConnectionandClient. -
zmq–ZMQ messaging objects using zmq sockets.
Classes:
-
LocalClient–A client for an interface running in the current process.
LocalClient
¶
Bases: HasInterface[T_interface_co], BaseClient[T_interface_co], Generic[T_interface_co]
A client for an interface running in the current process.
-
Reference
messaging
Connectionhandle_incoming_msg
Attributes:
-
connection(Fixed[Self, Connection[T_interface_co]]) –A local connection to the interface.
Source code in src/async_kernel/messaging/base.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
connection
class-attribute
instance-attribute
¶
connection: Fixed[Self, Connection[T_interface_co]] = Fixed(
lambda c: Connection(session_id=c["owner"].session_id)
)
A local connection to the interface.
Default Messaging object definitions for Messaging including BaseMessage, Connection and Client.
Classes:
-
BaseMessage–The base for messaging between kernel interfaces and clients.
-
Connection–Provides a connection to the interface for messaging.
-
BaseClient–Communicates with a single connection.
-
LocalClient–A client for an interface running in the current process.
BaseMessage
¶
Bases: StartStopTask, LoggingConfigurable, MessageProtocol
The base for messaging between kernel interfaces and clients.
- Reference messaging
Methods:
-
__init__–Initialize the instance.
-
send_message–Sends the message to the other side (client for kernel and vice versa) and returns a PendingMessage.
Attributes:
-
session_id(Fixed[Self, str]) –Used to identify this object as the
sessionin a message header. -
as_owner(Callable[[], Self]) –Provides a callable with reference to self.
Source code in src/async_kernel/messaging/base.py
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 | |
session_id
class-attribute
instance-attribute
¶
Used to identify this object as the session in a message header.
__init__
¶
__init__(
caller: Caller | None = None, /, session_id: str | NoValue = NoValue, **kwargs: Any
) -> None
Parameters:
-
(caller¶Caller | None, default:None) –The caller to use to run the interface.
-
(session_id¶str | NoValue, default:NoValue) –The id to use for to identify the instance in
msg["header"]["session"]. -
(**kwargs¶Any, default:{}) –Additional arguments to configure the instance.
Source code in src/async_kernel/messaging/base.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
send_message
¶
Sends the message to the other side (client for kernel and vice versa) and returns a PendingMessage.
Source code in src/async_kernel/messaging/base.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
Connection
¶
Bases: HasInterface[T_interface_co], BaseMessage, Generic[T_interface_co]
Provides a connection to the interface for messaging.
- Reference
-
Reference
messaging
ZMQConnection
Methods:
-
connection_task–Open the channels, set ready when ready block until stopped, Don't call directly.
-
handle_incoming_msg–The handler for messages received on this connection.
-
iopub_send–Publish an iopub message.
Source code in src/async_kernel/messaging/base.py
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 | |
connection_task
async
¶
Open the channels, set ready when ready block until stopped, Don't call directly.
Source code in src/async_kernel/messaging/base.py
191 192 193 194 195 196 197 | |
handle_incoming_msg
¶
The handler for messages received on this connection.
Parameters:
-
(msg¶Message) –A new message.
-
(ident¶list[bytes]) –A list of bytes to route a reply message back to the origin. This can be an empty list when there is only one connection, such as LocalClient.
Source code in src/async_kernel/messaging/base.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
iopub_send
¶
iopub_send(
msg_type: IOPubMsgTypeAlias | str,
content: Content | None = None,
*,
metadata: dict[str, Any] | None = None,
parent: dict[str, Any] | MsgHeader | NoValue | None = NoValue,
ident: bytes | list[bytes] | None = None,
buffers: BuffersType | None = None,
) -> None
Publish an iopub message.
Source code in src/async_kernel/messaging/base.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
BaseClient
¶
Bases: BaseMessage, Generic[T_interface_co]
Communicates with a single connection.
- Reference messaging
Methods:
-
input_request–Handle an
input_requestraised by the connected kernel. -
iopub_subscribe–Open a new iopub socket and subscribe to a particular topic.
-
execute–Execute code in the kernel.
-
complete–Tab complete text in the kernel's namespace.
-
inspect–Get metadata information about an object in the kernel's namespace.
-
history–Get entries from the kernel's history list.
-
kernel_info–Request kernel info.
-
comm_info–Request comm info.
-
is_complete–Ask the kernel whether some code is complete and ready to execute.
-
shutdown–Request an immediate kernel shutdown.
Attributes:
-
default_input_hander(Callable[[Content], CoroutineType[Any, Any, str]] | None) –The default handler for input requests.
Source code in src/async_kernel/messaging/base.py
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 | |
default_input_hander
class-attribute
instance-attribute
¶
default_input_hander: Callable[[Content], CoroutineType[Any, Any, str]] | None = (
traitlets.Callable(None, allow_none=True).tag(config=True)
)
The default handler for input requests.
input_request
async
¶
Handle an input_request raised by the connected kernel.
Source code in src/async_kernel/messaging/base.py
283 284 285 286 287 288 289 | |
iopub_subscribe
async
¶
iopub_subscribe(
topic: bytes = b"", *, timeout: float | None = 1
) -> AsyncGenerator[SingleAsyncQueue[Message]]
Open a new iopub socket and subscribe to a particular topic.
Parameters:
-
(topic¶bytes, default:b'') –The topics to subscribe to.
-
(timeout¶float | None, default:1) –The maximum time to wait for a welcome message.
Raise
TimeoutError: If a welcome message is not received in time.
Usaage:
async with client.iopub_subscribe() as queue:
async for msg in queue:
pass
Source code in src/async_kernel/messaging/base.py
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 | |
execute
¶
execute(
code: str,
silent: bool = False,
*,
store_history: bool = True,
user_expressions: dict[str, str] | None = None,
stop_on_error: NoValue | bool = NoValue,
metadata: dict[str, Any] | None = None,
input_handler: Callable[[Content], CoroutineType[Any, Any, str]]
| NoValue
| None = NoValue,
channel: Literal[shell, control] = shell,
subshell_id: str | None = None,
) -> PendingMessage
Execute code in the kernel.
Parameters:
-
(code¶str) –A string of code in the kernel's language.
-
(silent¶bool, default:False) –If set, the kernel will execute the code as quietly possible, and will force store_history to be False.
-
(store_history¶bool, default:True) –If set, the kernel will store command history. This is forced to be False if silent is True.
-
(user_expressions¶dict[str, str] | None, default:None) –A dict mapping names to expressions to be evaluated in the user's dict. The expression values are returned as strings formatted using repr.
-
(input_handler¶Callable[[Content], CoroutineType[Any, Any, str]] | NoValue | None, default:NoValue) –A handler for the stdin requests associated with the execute request. When not provided, stdin is disabled.
-
(stop_on_error¶NoValue | bool, default:NoValue) –Flag whether to abort the execution queue, if an exception is encountered.
Source code in src/async_kernel/messaging/base.py
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 | |
complete
¶
complete(code: str, cursor_pos: int | None = None) -> PendingMessage[Content]
Tab complete text in the kernel's namespace.
Parameters:
-
(code¶str) –The context in which completion is requested. Can be anything between a variable name and an entire cell.
-
(cursor_pos¶int | None, default:None) –The position of the cursor in the block of code where the completion was requested. Default:
len(code).
Source code in src/async_kernel/messaging/base.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
inspect
¶
inspect(
code: str, cursor_pos: int | None = None, detail_level: int = 0
) -> PendingMessage[Content]
Get metadata information about an object in the kernel's namespace.
It is up to the kernel to determine the appropriate object to inspect.
Parameters:
-
(code¶str) –Context in which info is requested. Can be anything between a variable name and an entire cell.
-
(cursor_pos¶int | None, default:None) –The position of the cursor in the block of code where the info was requested.
-
(detail_level¶int, default:0) –The level of detail for the introspection (0-2).
Source code in src/async_kernel/messaging/base.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
history
¶
history(
raw: bool = True,
output: bool = False,
hist_access_type: Literal["tail", "range", "search"] = "range",
**kwargs: Any,
) -> PendingMessage[Content]
Get entries from the kernel's history list.
Args: raw: If True, return the raw input. output: If True, then return the output as well. hist_access_type: 'range' (fill in session, start and stop params), 'tail' (fill in n) or 'search' (fill in pattern param). **kwargs: session: For a range request, the session from which to get lines. Session numbers are positive integers; negative ones count back from the current session. start: The first line number of a history range. stop: The final (excluded) line number of a history range. n: The number of lines of history to get for a tail request. pattern: The glob-syntax pattern for a search request.
Returns: The ID of the message sent.
Source code in src/async_kernel/messaging/base.py
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 | |
kernel_info
¶
kernel_info() -> PendingMessage[Content]
Request kernel info.
Source code in src/async_kernel/messaging/base.py
425 426 427 | |
comm_info
¶
Request comm info.
Source code in src/async_kernel/messaging/base.py
429 430 431 432 | |
is_complete
¶
Ask the kernel whether some code is complete and ready to execute.
Source code in src/async_kernel/messaging/base.py
434 435 436 | |
shutdown
¶
Request an immediate kernel shutdown.
Upon receipt of the (empty) reply, client code can safely assume that the kernel has shut down and it's safe to forcefully terminate it if it's still alive.
Source code in src/async_kernel/messaging/base.py
438 439 440 441 442 443 444 445 | |
LocalClient
¶
Bases: HasInterface[T_interface_co], BaseClient[T_interface_co], Generic[T_interface_co]
A client for an interface running in the current process.
-
Reference
messaging
Connectionhandle_incoming_msg
Attributes:
-
connection(Fixed[Self, Connection[T_interface_co]]) –A local connection to the interface.
Source code in src/async_kernel/messaging/base.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
connection
class-attribute
instance-attribute
¶
connection: Fixed[Self, Connection[T_interface_co]] = Fixed(
lambda c: Connection(session_id=c["owner"].session_id)
)
A local connection to the interface.
ZMQ messaging objects using zmq sockets.
Classes:
-
ZMQConnection–Provides the ZMQ sockets for clients to connect and communicate with the interface.
-
ZMQClient–A client for an interface that provides a ZMQConnection.
ZMQConnection
¶
Bases: ZMQMessage, Connection[T_interface_co], Generic[T_interface_co]
Provides the ZMQ sockets for clients to connect and communicate with the interface.
Source code in src/async_kernel/messaging/zmq.py
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 | |
ZMQClient
¶
Bases: BaseClient[T_interface_co], ZMQMessage, Generic[T_interface_co]
A client for an interface that provides a ZMQConnection.
The client can be connected to an existing interface's using either:
- ZMQClient.load_connection_info or,
- ZMQClient.load_connection_file
A new interface/kernel can be started with ZMQClient.subprocess_kernel.
Methods:
-
start–Connect this client to the interface.
-
subprocess_kernel–Start a kernel interface as a subprocess.
Attributes:
-
encryption–The type of encryption to use.
Source code in src/async_kernel/messaging/zmq.py
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 | |
encryption
class-attribute
instance-attribute
¶
encryption = traitlets.Enum(['curve'], default_value=None, allow_none=True)
The type of encryption to use.
start
¶
start(*, connect_timeout: float | None = None) -> Self
Connect this client to the interface.
Parameters:
-
(connect_timeout¶float | None, default:None) –The maximum time to wait for the connection to reply with a welcome message and to configure the session. passing
connect_timeout=0will skip the_establish_connectionstep.
Source code in src/async_kernel/messaging/zmq.py
304 305 306 307 308 309 310 311 312 313 314 315 | |
subprocess_kernel
async
¶
subprocess_kernel(
*,
connect_timeout: float | None = None,
heartbeat_interval: float | None = 10.0,
shutdown_timeout: float | None = 10.0,
**kwargs,
) -> AsyncGenerator[Popen]
Start a kernel interface as a subprocess.
Source code in src/async_kernel/messaging/zmq.py
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 | |