##// END OF EJS Templates
file_extension in language_info should include the .
Min RK -
Show More
@@ -1,1216 +1,1216 b''
1 1 .. _messaging:
2 2
3 3 ======================
4 4 Messaging in IPython
5 5 ======================
6 6
7 7
8 8 Versioning
9 9 ==========
10 10
11 11 The IPython message specification is versioned independently of IPython.
12 12 The current version of the specification is 5.0.
13 13
14 14
15 15 Introduction
16 16 ============
17 17
18 18 This document explains the basic communications design and messaging
19 19 specification for how the various IPython objects interact over a network
20 20 transport. The current implementation uses the ZeroMQ_ library for messaging
21 21 within and between hosts.
22 22
23 23 .. Note::
24 24
25 25 This document should be considered the authoritative description of the
26 26 IPython messaging protocol, and all developers are strongly encouraged to
27 27 keep it updated as the implementation evolves, so that we have a single
28 28 common reference for all protocol details.
29 29
30 30 The basic design is explained in the following diagram:
31 31
32 32 .. image:: figs/frontend-kernel.png
33 33 :width: 450px
34 34 :alt: IPython kernel/frontend messaging architecture.
35 35 :align: center
36 36 :target: ../_images/frontend-kernel.png
37 37
38 38 A single kernel can be simultaneously connected to one or more frontends. The
39 39 kernel has three sockets that serve the following functions:
40 40
41 41 1. Shell: this single ROUTER socket allows multiple incoming connections from
42 42 frontends, and this is the socket where requests for code execution, object
43 43 information, prompts, etc. are made to the kernel by any frontend. The
44 44 communication on this socket is a sequence of request/reply actions from
45 45 each frontend and the kernel.
46 46
47 47 2. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
48 48 side effects (stdout, stderr, etc.) as well as the requests coming from any
49 49 client over the shell socket and its own requests on the stdin socket. There
50 50 are a number of actions in Python which generate side effects: :func:`print`
51 51 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
52 52 a multi-client scenario, we want all frontends to be able to know what each
53 53 other has sent to the kernel (this can be useful in collaborative scenarios,
54 54 for example). This socket allows both side effects and the information
55 55 about communications taking place with one client over the shell channel
56 56 to be made available to all clients in a uniform manner.
57 57
58 58 3. stdin: this ROUTER socket is connected to all frontends, and it allows
59 59 the kernel to request input from the active frontend when :func:`raw_input` is called.
60 60 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
61 61 for the kernel while this communication is happening (illustrated in the
62 62 figure by the black outline around the central keyboard). In practice,
63 63 frontends may display such kernel requests using a special input widget or
64 64 otherwise indicating that the user is to type input for the kernel instead
65 65 of normal commands in the frontend.
66 66
67 67 All messages are tagged with enough information (details below) for clients
68 68 to know which messages come from their own interaction with the kernel and
69 69 which ones are from other clients, so they can display each type
70 70 appropriately.
71 71
72 72 4. Control: This channel is identical to Shell, but operates on a separate socket,
73 73 to allow important messages to avoid queueing behind execution requests (e.g. shutdown or abort).
74 74
75 75 The actual format of the messages allowed on each of these channels is
76 76 specified below. Messages are dicts of dicts with string keys and values that
77 77 are reasonably representable in JSON. Our current implementation uses JSON
78 78 explicitly as its message format, but this shouldn't be considered a permanent
79 79 feature. As we've discovered that JSON has non-trivial performance issues due
80 80 to excessive copying, we may in the future move to a pure pickle-based raw
81 81 message format. However, it should be possible to easily convert from the raw
82 82 objects to JSON, since we may have non-python clients (e.g. a web frontend).
83 83 As long as it's easy to make a JSON version of the objects that is a faithful
84 84 representation of all the data, we can communicate with such clients.
85 85
86 86 .. Note::
87 87
88 88 Not all of these have yet been fully fleshed out, but the key ones are, see
89 89 kernel and frontend files for actual implementation details.
90 90
91 91 General Message Format
92 92 ======================
93 93
94 94 A message is defined by the following four-dictionary structure::
95 95
96 96 {
97 97 # The message header contains a pair of unique identifiers for the
98 98 # originating session and the actual message id, in addition to the
99 99 # username for the process that generated the message. This is useful in
100 100 # collaborative settings where multiple users may be interacting with the
101 101 # same kernel simultaneously, so that frontends can label the various
102 102 # messages in a meaningful way.
103 103 'header' : {
104 104 'msg_id' : uuid,
105 105 'username' : str,
106 106 'session' : uuid,
107 107 # All recognized message type strings are listed below.
108 108 'msg_type' : str,
109 109 # the message protocol version
110 110 'version' : '5.0',
111 111 },
112 112
113 113 # In a chain of messages, the header from the parent is copied so that
114 114 # clients can track where messages come from.
115 115 'parent_header' : dict,
116 116
117 117 # Any metadata associated with the message.
118 118 'metadata' : dict,
119 119
120 120 # The actual content of the message must be a dict, whose structure
121 121 # depends on the message type.
122 122 'content' : dict,
123 123 }
124 124
125 125 .. versionchanged:: 5.0
126 126
127 127 ``version`` key added to the header.
128 128
129 129 .. _wire_protocol:
130 130
131 131 The Wire Protocol
132 132 =================
133 133
134 134
135 135 This message format exists at a high level,
136 136 but does not describe the actual *implementation* at the wire level in zeromq.
137 137 The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class.
138 138
139 139 .. note::
140 140
141 141 This section should only be relevant to non-Python consumers of the protocol.
142 142 Python consumers should simply import and use IPython's own implementation of the wire protocol
143 143 in the :class:`IPython.kernel.zmq.session.Session` object.
144 144
145 145 Every message is serialized to a sequence of at least six blobs of bytes:
146 146
147 147 .. sourcecode:: python
148 148
149 149 [
150 150 b'u-u-i-d', # zmq identity(ies)
151 151 b'<IDS|MSG>', # delimiter
152 152 b'baddad42', # HMAC signature
153 153 b'{header}', # serialized header dict
154 154 b'{parent_header}', # serialized parent header dict
155 155 b'{metadata}', # serialized metadata dict
156 156 b'{content}', # serialized content dict
157 157 b'blob', # extra raw data buffer(s)
158 158 ...
159 159 ]
160 160
161 161 The front of the message is the ZeroMQ routing prefix,
162 162 which can be zero or more socket identities.
163 163 This is every piece of the message prior to the delimiter key ``<IDS|MSG>``.
164 164 In the case of IOPub, there should be just one prefix component,
165 165 which is the topic for IOPub subscribers, e.g. ``execute_result``, ``display_data``.
166 166
167 167 .. note::
168 168
169 169 In most cases, the IOPub topics are irrelevant and completely ignored,
170 170 because frontends just subscribe to all topics.
171 171 The convention used in the IPython kernel is to use the msg_type as the topic,
172 172 and possibly extra information about the message, e.g. ``execute_result`` or ``stream.stdout``
173 173
174 174 After the delimiter is the `HMAC`_ signature of the message, used for authentication.
175 175 If authentication is disabled, this should be an empty string.
176 176 By default, the hashing function used for computing these signatures is sha256.
177 177
178 178 .. _HMAC: http://en.wikipedia.org/wiki/HMAC
179 179
180 180 .. note::
181 181
182 182 To disable authentication and signature checking,
183 183 set the `key` field of a connection file to an empty string.
184 184
185 185 The signature is the HMAC hex digest of the concatenation of:
186 186
187 187 - A shared key (typically the ``key`` field of a connection file)
188 188 - The serialized header dict
189 189 - The serialized parent header dict
190 190 - The serialized metadata dict
191 191 - The serialized content dict
192 192
193 193 In Python, this is implemented via:
194 194
195 195 .. sourcecode:: python
196 196
197 197 # once:
198 198 digester = HMAC(key, digestmod=hashlib.sha256)
199 199
200 200 # for each message
201 201 d = digester.copy()
202 202 for serialized_dict in (header, parent, metadata, content):
203 203 d.update(serialized_dict)
204 204 signature = d.hexdigest()
205 205
206 206 After the signature is the actual message, always in four frames of bytes.
207 207 The four dictionaries that compose a message are serialized separately,
208 208 in the order of header, parent header, metadata, and content.
209 209 These can be serialized by any function that turns a dict into bytes.
210 210 The default and most common serialization is JSON, but msgpack and pickle
211 211 are common alternatives.
212 212
213 213 After the serialized dicts are zero to many raw data buffers,
214 214 which can be used by message types that support binary data (mainly apply and data_pub).
215 215
216 216
217 217 Python functional API
218 218 =====================
219 219
220 220 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
221 221 should develop, at a few key points, functional forms of all the requests that
222 222 take arguments in this manner and automatically construct the necessary dict
223 223 for sending.
224 224
225 225 In addition, the Python implementation of the message specification extends
226 226 messages upon deserialization to the following form for convenience::
227 227
228 228 {
229 229 'header' : dict,
230 230 # The msg's unique identifier and type are always stored in the header,
231 231 # but the Python implementation copies them to the top level.
232 232 'msg_id' : uuid,
233 233 'msg_type' : str,
234 234 'parent_header' : dict,
235 235 'content' : dict,
236 236 'metadata' : dict,
237 237 }
238 238
239 239 All messages sent to or received by any IPython process should have this
240 240 extended structure.
241 241
242 242
243 243 Messages on the shell ROUTER/DEALER sockets
244 244 ===========================================
245 245
246 246 .. _execute:
247 247
248 248 Execute
249 249 -------
250 250
251 251 This message type is used by frontends to ask the kernel to execute code on
252 252 behalf of the user, in a namespace reserved to the user's variables (and thus
253 253 separate from the kernel's own internal code and variables).
254 254
255 255 Message type: ``execute_request``::
256 256
257 257 content = {
258 258 # Source code to be executed by the kernel, one or more lines.
259 259 'code' : str,
260 260
261 261 # A boolean flag which, if True, signals the kernel to execute
262 262 # this code as quietly as possible.
263 263 # silent=True forces store_history to be False,
264 264 # and will *not*:
265 265 # - broadcast output on the IOPUB channel
266 266 # - have an execute_result
267 267 # The default is False.
268 268 'silent' : bool,
269 269
270 270 # A boolean flag which, if True, signals the kernel to populate history
271 271 # The default is True if silent is False. If silent is True, store_history
272 272 # is forced to be False.
273 273 'store_history' : bool,
274 274
275 275 # A dict mapping names to expressions to be evaluated in the
276 276 # user's dict. The rich display-data representation of each will be evaluated after execution.
277 277 # See the display_data content for the structure of the representation data.
278 278 'user_expressions' : dict,
279 279
280 280 # Some frontends do not support stdin requests.
281 281 # If raw_input is called from code executed from such a frontend,
282 282 # a StdinNotImplementedError will be raised.
283 283 'allow_stdin' : True,
284 284
285 285 # A boolean flag, which, if True, does not abort the execution queue, if an exception is encountered.
286 286 # This allows the queued execution of multiple execute_requests, even if they generate exceptions.
287 287 'stop_on_error' : False,
288 288 }
289 289
290 290 .. versionchanged:: 5.0
291 291
292 292 ``user_variables`` removed, because it is redundant with user_expressions.
293 293
294 294 The ``code`` field contains a single string (possibly multiline) to be executed.
295 295
296 296 The ``user_expressions`` field deserves a detailed explanation. In the past, IPython had
297 297 the notion of a prompt string that allowed arbitrary code to be evaluated, and
298 298 this was put to good use by many in creating prompts that displayed system
299 299 status, path information, and even more esoteric uses like remote instrument
300 300 status acquired over the network. But now that IPython has a clean separation
301 301 between the kernel and the clients, the kernel has no prompt knowledge; prompts
302 302 are a frontend feature, and it should be even possible for different
303 303 frontends to display different prompts while interacting with the same kernel.
304 304 ``user_expressions`` can be used to retrieve this information.
305 305
306 306 Any error in evaluating any expression in ``user_expressions`` will result in
307 307 only that key containing a standard error message, of the form::
308 308
309 309 {
310 310 'status' : 'error',
311 311 'ename' : 'NameError',
312 312 'evalue' : 'foo',
313 313 'traceback' : ...
314 314 }
315 315
316 316 .. Note::
317 317
318 318 In order to obtain the current execution counter for the purposes of
319 319 displaying input prompts, frontends may make an execution request with an
320 320 empty code string and ``silent=True``.
321 321
322 322 Upon completion of the execution request, the kernel *always* sends a reply,
323 323 with a status code indicating what happened and additional data depending on
324 324 the outcome. See :ref:`below <execution_results>` for the possible return
325 325 codes and associated data.
326 326
327 327 .. seealso::
328 328
329 329 :ref:`execution_semantics`
330 330
331 331 .. _execution_counter:
332 332
333 333 Execution counter (prompt number)
334 334 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
335 335
336 336 The kernel should have a single, monotonically increasing counter of all execution
337 337 requests that are made with ``store_history=True``. This counter is used to populate
338 338 the ``In[n]`` and ``Out[n]`` prompts. The value of this counter will be returned as the
339 339 ``execution_count`` field of all ``execute_reply`` and ``execute_input`` messages.
340 340
341 341 .. _execution_results:
342 342
343 343 Execution results
344 344 ~~~~~~~~~~~~~~~~~
345 345
346 346 Message type: ``execute_reply``::
347 347
348 348 content = {
349 349 # One of: 'ok' OR 'error' OR 'abort'
350 350 'status' : str,
351 351
352 352 # The global kernel counter that increases by one with each request that
353 353 # stores history. This will typically be used by clients to display
354 354 # prompt numbers to the user. If the request did not store history, this will
355 355 # be the current value of the counter in the kernel.
356 356 'execution_count' : int,
357 357 }
358 358
359 359 When status is 'ok', the following extra fields are present::
360 360
361 361 {
362 362 # 'payload' will be a list of payload dicts, and is optional.
363 363 # payloads are considered deprecated.
364 364 # The only requirement of each payload dict is that it have a 'source' key,
365 365 # which is a string classifying the payload (e.g. 'page').
366 366
367 367 'payload' : list(dict),
368 368
369 369 # Results for the user_expressions.
370 370 'user_expressions' : dict,
371 371 }
372 372
373 373 .. versionchanged:: 5.0
374 374
375 375 ``user_variables`` is removed, use user_expressions instead.
376 376
377 377 When status is 'error', the following extra fields are present::
378 378
379 379 {
380 380 'ename' : str, # Exception name, as a string
381 381 'evalue' : str, # Exception value, as a string
382 382
383 383 # The traceback will contain a list of frames, represented each as a
384 384 # string. For now we'll stick to the existing design of ultraTB, which
385 385 # controls exception level of detail statefully. But eventually we'll
386 386 # want to grow into a model where more information is collected and
387 387 # packed into the traceback object, with clients deciding how little or
388 388 # how much of it to unpack. But for now, let's start with a simple list
389 389 # of strings, since that requires only minimal changes to ultratb as
390 390 # written.
391 391 'traceback' : list,
392 392 }
393 393
394 394
395 395 When status is 'abort', there are for now no additional data fields. This
396 396 happens when the kernel was interrupted by a signal.
397 397
398 398 Payloads
399 399 ********
400 400
401 401 .. admonition:: Execution payloads
402 402
403 403 Payloads are considered deprecated, though their replacement is not yet implemented.
404 404
405 405 Payloads are a way to trigger frontend actions from the kernel. Current payloads:
406 406
407 407 **page**: display data in a pager.
408 408
409 409 Pager output is used for introspection, or other displayed information that's not considered output.
410 410 Pager payloads are generally displayed in a separate pane, that can be viewed alongside code,
411 411 and are not included in notebook documents.
412 412
413 413 .. sourcecode:: python
414 414
415 415 {
416 416 "source": "page",
417 417 # mime-bundle of data to display in the pager.
418 418 # Must include text/plain.
419 419 "data": mimebundle,
420 420 # line offset to start from
421 421 "start": int,
422 422 }
423 423
424 424 **set_next_input**: create a new output
425 425
426 426 used to create new cells in the notebook,
427 427 or set the next input in a console interface.
428 428 The main example being ``%load``.
429 429
430 430 .. sourcecode:: python
431 431
432 432 {
433 433 "source": "set_next_input",
434 434 # the text contents of the cell to create
435 435 "text": "some cell content",
436 436 # If true, replace the current cell in document UIs instead of inserting
437 437 # a cell. Ignored in console UIs.
438 438 "replace": bool,
439 439 }
440 440
441 441 **edit**: open a file for editing.
442 442
443 443 Triggered by `%edit`. Only the QtConsole currently supports edit payloads.
444 444
445 445 .. sourcecode:: python
446 446
447 447 {
448 448 "source": "edit",
449 449 "filename": "/path/to/file.py", # the file to edit
450 450 "line_number": int, # the line number to start with
451 451 }
452 452
453 453 **ask_exit**: instruct the frontend to prompt the user for exit
454 454
455 455 Allows the kernel to request exit, e.g. via ``%exit`` in IPython.
456 456 Only for console frontends.
457 457
458 458 .. sourcecode:: python
459 459
460 460 {
461 461 "source": "ask_exit",
462 462 # whether the kernel should be left running, only closing the client
463 463 "keepkernel": bool,
464 464 }
465 465
466 466
467 467 .. _msging_inspection:
468 468
469 469 Introspection
470 470 -------------
471 471
472 472 Code can be inspected to show useful information to the user.
473 473 It is up to the Kernel to decide what information should be displayed, and its formatting.
474 474
475 475 Message type: ``inspect_request``::
476 476
477 477 content = {
478 478 # The code context in which introspection is requested
479 479 # this may be up to an entire multiline cell.
480 480 'code' : str,
481 481
482 482 # The cursor position within 'code' (in unicode characters) where inspection is requested
483 483 'cursor_pos' : int,
484 484
485 485 # The level of detail desired. In IPython, the default (0) is equivalent to typing
486 486 # 'x?' at the prompt, 1 is equivalent to 'x??'.
487 487 # The difference is up to kernels, but in IPython level 1 includes the source code
488 488 # if available.
489 489 'detail_level' : 0 or 1,
490 490 }
491 491
492 492 .. versionchanged:: 5.0
493 493
494 494 ``object_info_request`` renamed to ``inspect_request``.
495 495
496 496 .. versionchanged:: 5.0
497 497
498 498 ``name`` key replaced with ``code`` and ``cursor_pos``,
499 499 moving the lexing responsibility to the kernel.
500 500
501 501 The reply is a mime-bundle, like a `display_data`_ message,
502 502 which should be a formatted representation of information about the context.
503 503 In the notebook, this is used to show tooltips over function calls, etc.
504 504
505 505 Message type: ``inspect_reply``::
506 506
507 507 content = {
508 508 # 'ok' if the request succeeded or 'error', with error information as in all other replies.
509 509 'status' : 'ok',
510 510
511 511 # data can be empty if nothing is found
512 512 'data' : dict,
513 513 'metadata' : dict,
514 514 }
515 515
516 516 .. versionchanged:: 5.0
517 517
518 518 ``object_info_reply`` renamed to ``inspect_reply``.
519 519
520 520 .. versionchanged:: 5.0
521 521
522 522 Reply is changed from structured data to a mime bundle, allowing formatting decisions to be made by the kernel.
523 523
524 524 .. _msging_completion:
525 525
526 526 Completion
527 527 ----------
528 528
529 529 Message type: ``complete_request``::
530 530
531 531 content = {
532 532 # The code context in which completion is requested
533 533 # this may be up to an entire multiline cell, such as
534 534 # 'foo = a.isal'
535 535 'code' : str,
536 536
537 537 # The cursor position within 'code' (in unicode characters) where completion is requested
538 538 'cursor_pos' : int,
539 539 }
540 540
541 541 .. versionchanged:: 5.0
542 542
543 543 ``line``, ``block``, and ``text`` keys are removed in favor of a single ``code`` for context.
544 544 Lexing is up to the kernel.
545 545
546 546
547 547 Message type: ``complete_reply``::
548 548
549 549 content = {
550 550 # The list of all matches to the completion request, such as
551 551 # ['a.isalnum', 'a.isalpha'] for the above example.
552 552 'matches' : list,
553 553
554 554 # The range of text that should be replaced by the above matches when a completion is accepted.
555 555 # typically cursor_end is the same as cursor_pos in the request.
556 556 'cursor_start' : int,
557 557 'cursor_end' : int,
558 558
559 559 # Information that frontend plugins might use for extra display information about completions.
560 560 'metadata' : dict,
561 561
562 562 # status should be 'ok' unless an exception was raised during the request,
563 563 # in which case it should be 'error', along with the usual error message content
564 564 # in other messages.
565 565 'status' : 'ok'
566 566 }
567 567
568 568 .. versionchanged:: 5.0
569 569
570 570 - ``matched_text`` is removed in favor of ``cursor_start`` and ``cursor_end``.
571 571 - ``metadata`` is added for extended information.
572 572
573 573 .. _msging_history:
574 574
575 575 History
576 576 -------
577 577
578 578 For clients to explicitly request history from a kernel. The kernel has all
579 579 the actual execution history stored in a single location, so clients can
580 580 request it from the kernel when needed.
581 581
582 582 Message type: ``history_request``::
583 583
584 584 content = {
585 585
586 586 # If True, also return output history in the resulting dict.
587 587 'output' : bool,
588 588
589 589 # If True, return the raw input history, else the transformed input.
590 590 'raw' : bool,
591 591
592 592 # So far, this can be 'range', 'tail' or 'search'.
593 593 'hist_access_type' : str,
594 594
595 595 # If hist_access_type is 'range', get a range of input cells. session can
596 596 # be a positive session number, or a negative number to count back from
597 597 # the current session.
598 598 'session' : int,
599 599 # start and stop are line numbers within that session.
600 600 'start' : int,
601 601 'stop' : int,
602 602
603 603 # If hist_access_type is 'tail' or 'search', get the last n cells.
604 604 'n' : int,
605 605
606 606 # If hist_access_type is 'search', get cells matching the specified glob
607 607 # pattern (with * and ? as wildcards).
608 608 'pattern' : str,
609 609
610 610 # If hist_access_type is 'search' and unique is true, do not
611 611 # include duplicated history. Default is false.
612 612 'unique' : bool,
613 613
614 614 }
615 615
616 616 .. versionadded:: 4.0
617 617 The key ``unique`` for ``history_request``.
618 618
619 619 Message type: ``history_reply``::
620 620
621 621 content = {
622 622 # A list of 3 tuples, either:
623 623 # (session, line_number, input) or
624 624 # (session, line_number, (input, output)),
625 625 # depending on whether output was False or True, respectively.
626 626 'history' : list,
627 627 }
628 628
629 629 .. _msging_is_complete:
630 630
631 631 Code completeness
632 632 -----------------
633 633
634 634 .. versionadded:: 5.0
635 635
636 636 When the user enters a line in a console style interface, the console must
637 637 decide whether to immediately execute the current code, or whether to show a
638 638 continuation prompt for further input. For instance, in Python ``a = 5`` would
639 639 be executed immediately, while ``for i in range(5):`` would expect further input.
640 640
641 641 There are four possible replies:
642 642
643 643 - *complete* code is ready to be executed
644 644 - *incomplete* code should prompt for another line
645 645 - *invalid* code will typically be sent for execution, so that the user sees the
646 646 error soonest.
647 647 - *unknown* - if the kernel is not able to determine this. The frontend should
648 648 also handle the kernel not replying promptly. It may default to sending the
649 649 code for execution, or it may implement simple fallback heuristics for whether
650 650 to execute the code (e.g. execute after a blank line).
651 651
652 652 Frontends may have ways to override this, forcing the code to be sent for
653 653 execution or forcing a continuation prompt.
654 654
655 655 Message type: ``is_complete_request``::
656 656
657 657 content = {
658 658 # The code entered so far as a multiline string
659 659 'code' : str,
660 660 }
661 661
662 662 Message type: ``is_complete_reply``::
663 663
664 664 content = {
665 665 # One of 'complete', 'incomplete', 'invalid', 'unknown'
666 666 'status' : str,
667 667
668 668 # If status is 'incomplete', indent should contain the characters to use
669 669 # to indent the next line. This is only a hint: frontends may ignore it
670 670 # and use their own autoindentation rules. For other statuses, this
671 671 # field does not exist.
672 672 'indent': str,
673 673 }
674 674
675 675 Connect
676 676 -------
677 677
678 678 When a client connects to the request/reply socket of the kernel, it can issue
679 679 a connect request to get basic information about the kernel, such as the ports
680 680 the other ZeroMQ sockets are listening on. This allows clients to only have
681 681 to know about a single port (the shell channel) to connect to a kernel.
682 682
683 683 Message type: ``connect_request``::
684 684
685 685 content = {
686 686 }
687 687
688 688 Message type: ``connect_reply``::
689 689
690 690 content = {
691 691 'shell_port' : int, # The port the shell ROUTER socket is listening on.
692 692 'iopub_port' : int, # The port the PUB socket is listening on.
693 693 'stdin_port' : int, # The port the stdin ROUTER socket is listening on.
694 694 'hb_port' : int, # The port the heartbeat socket is listening on.
695 695 }
696 696
697 697 .. _msging_kernel_info:
698 698
699 699 Kernel info
700 700 -----------
701 701
702 702 If a client needs to know information about the kernel, it can
703 703 make a request of the kernel's information.
704 704 This message can be used to fetch core information of the
705 705 kernel, including language (e.g., Python), language version number and
706 706 IPython version number, and the IPython message spec version number.
707 707
708 708 Message type: ``kernel_info_request``::
709 709
710 710 content = {
711 711 }
712 712
713 713 Message type: ``kernel_info_reply``::
714 714
715 715 content = {
716 716 # Version of messaging protocol.
717 717 # The first integer indicates major version. It is incremented when
718 718 # there is any backward incompatible change.
719 719 # The second integer indicates minor version. It is incremented when
720 720 # there is any backward compatible change.
721 721 'protocol_version': 'X.Y.Z',
722 722
723 723 # The kernel implementation name
724 724 # (e.g. 'ipython' for the IPython kernel)
725 725 'implementation': str,
726 726
727 727 # Implementation version number.
728 728 # The version number of the kernel's implementation
729 729 # (e.g. IPython.__version__ for the IPython kernel)
730 730 'implementation_version': 'X.Y.Z',
731 731
732 732 # Information about the language of code for the kernel
733 733 'language_info': {
734 734 # Name of the programming language in which kernel is implemented.
735 735 # Kernel included in IPython returns 'python'.
736 736 'name': str,
737 737
738 738 # Language version number.
739 739 # It is Python version number (e.g., '2.7.3') for the kernel
740 740 # included in IPython.
741 741 'version': 'X.Y.Z',
742 742
743 743 # mimetype for script files in this language
744 744 'mimetype': str,
745 745
746 # Extension without the dot, e.g. 'py'
746 # Extension including the dot, e.g. '.py'
747 747 'file_extension': str,
748 748
749 749 # Pygments lexer, for highlighting
750 750 # Only needed if it differs from the top level 'language' field.
751 751 'pygments_lexer': str,
752 752
753 753 # Codemirror mode, for for highlighting in the notebook.
754 754 # Only needed if it differs from the top level 'language' field.
755 755 'codemirror_mode': str or dict,
756 756
757 757 # Nbconvert exporter, if notebooks written with this kernel should
758 758 # be exported with something other than the general 'script'
759 759 # exporter.
760 760 'nbconvert_exporter': str,
761 761 },
762 762
763 763 # A banner of information about the kernel,
764 764 # which may be desplayed in console environments.
765 765 'banner' : str,
766 766
767 767 # Optional: A list of dictionaries, each with keys 'text' and 'url'.
768 768 # These will be displayed in the help menu in the notebook UI.
769 769 'help_links': [
770 770 {'text': str, 'url': str}
771 771 ],
772 772 }
773 773
774 774 Refer to the lists of available `Pygments lexers <http://pygments.org/docs/lexers/>`_
775 775 and `codemirror modes <http://codemirror.net/mode/index.html>`_ for those fields.
776 776
777 777 .. versionchanged:: 5.0
778 778
779 779 Versions changed from lists of integers to strings.
780 780
781 781 .. versionchanged:: 5.0
782 782
783 783 ``ipython_version`` is removed.
784 784
785 785 .. versionchanged:: 5.0
786 786
787 787 ``language_info``, ``implementation``, ``implementation_version``, ``banner``
788 788 and ``help_links`` keys are added.
789 789
790 790 .. versionchanged:: 5.0
791 791
792 792 ``language_version`` moved to ``language_info.version``
793 793
794 794 .. versionchanged:: 5.0
795 795
796 796 ``language`` moved to ``language_info.name``
797 797
798 798 .. _msging_shutdown:
799 799
800 800 Kernel shutdown
801 801 ---------------
802 802
803 803 The clients can request the kernel to shut itself down; this is used in
804 804 multiple cases:
805 805
806 806 - when the user chooses to close the client application via a menu or window
807 807 control.
808 808 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
809 809 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
810 810 IPythonQt client) to force a kernel restart to get a clean kernel without
811 811 losing client-side state like history or inlined figures.
812 812
813 813 The client sends a shutdown request to the kernel, and once it receives the
814 814 reply message (which is otherwise empty), it can assume that the kernel has
815 815 completed shutdown safely.
816 816
817 817 Upon their own shutdown, client applications will typically execute a last
818 818 minute sanity check and forcefully terminate any kernel that is still alive, to
819 819 avoid leaving stray processes in the user's machine.
820 820
821 821 Message type: ``shutdown_request``::
822 822
823 823 content = {
824 824 'restart' : bool # whether the shutdown is final, or precedes a restart
825 825 }
826 826
827 827 Message type: ``shutdown_reply``::
828 828
829 829 content = {
830 830 'restart' : bool # whether the shutdown is final, or precedes a restart
831 831 }
832 832
833 833 .. Note::
834 834
835 835 When the clients detect a dead kernel thanks to inactivity on the heartbeat
836 836 socket, they simply send a forceful process termination signal, since a dead
837 837 process is unlikely to respond in any useful way to messages.
838 838
839 839
840 840 Messages on the PUB/SUB socket
841 841 ==============================
842 842
843 843 Streams (stdout, stderr, etc)
844 844 ------------------------------
845 845
846 846 Message type: ``stream``::
847 847
848 848 content = {
849 849 # The name of the stream is one of 'stdout', 'stderr'
850 850 'name' : str,
851 851
852 852 # The text is an arbitrary string to be written to that stream
853 853 'text' : str,
854 854 }
855 855
856 856 .. versionchanged:: 5.0
857 857
858 858 'data' key renamed to 'text' for conistency with the notebook format.
859 859
860 860 Display Data
861 861 ------------
862 862
863 863 This type of message is used to bring back data that should be displayed (text,
864 864 html, svg, etc.) in the frontends. This data is published to all frontends.
865 865 Each message can have multiple representations of the data; it is up to the
866 866 frontend to decide which to use and how. A single message should contain all
867 867 possible representations of the same information. Each representation should
868 868 be a JSON'able data structure, and should be a valid MIME type.
869 869
870 870 Some questions remain about this design:
871 871
872 872 * Do we use this message type for execute_result/displayhook? Probably not, because
873 873 the displayhook also has to handle the Out prompt display. On the other hand
874 874 we could put that information into the metadata section.
875 875
876 876 .. _display_data:
877 877
878 878 Message type: ``display_data``::
879 879
880 880 content = {
881 881
882 882 # Who create the data
883 883 'source' : str,
884 884
885 885 # The data dict contains key/value pairs, where the keys are MIME
886 886 # types and the values are the raw data of the representation in that
887 887 # format.
888 888 'data' : dict,
889 889
890 890 # Any metadata that describes the data
891 891 'metadata' : dict
892 892 }
893 893
894 894
895 895 The ``metadata`` contains any metadata that describes the output.
896 896 Global keys are assumed to apply to the output as a whole.
897 897 The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries,
898 898 which are interpreted as applying only to output of that type.
899 899 Third parties should put any data they write into a single dict
900 900 with a reasonably unique name to avoid conflicts.
901 901
902 902 The only metadata keys currently defined in IPython are the width and height
903 903 of images::
904 904
905 905 metadata = {
906 906 'image/png' : {
907 907 'width': 640,
908 908 'height': 480
909 909 }
910 910 }
911 911
912 912
913 913 .. versionchanged:: 5.0
914 914
915 915 `application/json` data should be unpacked JSON data,
916 916 not double-serialized as a JSON string.
917 917
918 918
919 919 Raw Data Publication
920 920 --------------------
921 921
922 922 ``display_data`` lets you publish *representations* of data, such as images and html.
923 923 This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers.
924 924
925 925 data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function:
926 926
927 927 .. sourcecode:: python
928 928
929 929 from IPython.kernel.zmq.datapub import publish_data
930 930 ns = dict(x=my_array)
931 931 publish_data(ns)
932 932
933 933
934 934 Message type: ``data_pub``::
935 935
936 936 content = {
937 937 # the keys of the data dict, after it has been unserialized
938 938 'keys' : ['a', 'b']
939 939 }
940 940 # the namespace dict will be serialized in the message buffers,
941 941 # which will have a length of at least one
942 942 buffers = [b'pdict', ...]
943 943
944 944
945 945 The interpretation of a sequence of data_pub messages for a given parent request should be
946 946 to update a single namespace with subsequent results.
947 947
948 948 .. note::
949 949
950 950 No frontends directly handle data_pub messages at this time.
951 951 It is currently only used by the client/engines in :mod:`IPython.parallel`,
952 952 where engines may publish *data* to the Client,
953 953 of which the Client can then publish *representations* via ``display_data``
954 954 to various frontends.
955 955
956 956 Code inputs
957 957 -----------
958 958
959 959 To let all frontends know what code is being executed at any given time, these
960 960 messages contain a re-broadcast of the ``code`` portion of an
961 961 :ref:`execute_request <execute>`, along with the :ref:`execution_count
962 962 <execution_counter>`.
963 963
964 964 Message type: ``execute_input``::
965 965
966 966 content = {
967 967 'code' : str, # Source code to be executed, one or more lines
968 968
969 969 # The counter for this execution is also provided so that clients can
970 970 # display it, since IPython automatically creates variables called _iN
971 971 # (for input prompt In[N]).
972 972 'execution_count' : int
973 973 }
974 974
975 975 .. versionchanged:: 5.0
976 976
977 977 ``pyin`` is renamed to ``execute_input``.
978 978
979 979
980 980 Execution results
981 981 -----------------
982 982
983 983 Results of an execution are published as an ``execute_result``.
984 984 These are identical to `display_data`_ messages, with the addition of an ``execution_count`` key.
985 985
986 986 Results can have multiple simultaneous formats depending on its
987 987 configuration. A plain text representation should always be provided
988 988 in the ``text/plain`` mime-type. Frontends are free to display any or all of these
989 989 according to its capabilities.
990 990 Frontends should ignore mime-types they do not understand. The data itself is
991 991 any JSON object and depends on the format. It is often, but not always a string.
992 992
993 993 Message type: ``execute_result``::
994 994
995 995 content = {
996 996
997 997 # The counter for this execution is also provided so that clients can
998 998 # display it, since IPython automatically creates variables called _N
999 999 # (for prompt N).
1000 1000 'execution_count' : int,
1001 1001
1002 1002 # data and metadata are identical to a display_data message.
1003 1003 # the object being displayed is that passed to the display hook,
1004 1004 # i.e. the *result* of the execution.
1005 1005 'data' : dict,
1006 1006 'metadata' : dict,
1007 1007 }
1008 1008
1009 1009 Execution errors
1010 1010 ----------------
1011 1011
1012 1012 When an error occurs during code execution
1013 1013
1014 1014 Message type: ``error``::
1015 1015
1016 1016 content = {
1017 1017 # Similar content to the execute_reply messages for the 'error' case,
1018 1018 # except the 'status' field is omitted.
1019 1019 }
1020 1020
1021 1021 .. versionchanged:: 5.0
1022 1022
1023 1023 ``pyerr`` renamed to ``error``
1024 1024
1025 1025 Kernel status
1026 1026 -------------
1027 1027
1028 1028 This message type is used by frontends to monitor the status of the kernel.
1029 1029
1030 1030 Message type: ``status``::
1031 1031
1032 1032 content = {
1033 1033 # When the kernel starts to handle a message, it will enter the 'busy'
1034 1034 # state and when it finishes, it will enter the 'idle' state.
1035 1035 # The kernel will publish state 'starting' exactly once at process startup.
1036 1036 execution_state : ('busy', 'idle', 'starting')
1037 1037 }
1038 1038
1039 1039 .. versionchanged:: 5.0
1040 1040
1041 1041 Busy and idle messages should be sent before/after handling every message,
1042 1042 not just execution.
1043 1043
1044 1044 .. note::
1045 1045
1046 1046 Extra status messages are added between the notebook webserver and websocket clients
1047 1047 that are not sent by the kernel. These are:
1048 1048
1049 1049 - restarting (kernel has died, but will be automatically restarted)
1050 1050 - dead (kernel has died, restarting has failed)
1051 1051
1052 1052 Clear output
1053 1053 ------------
1054 1054
1055 1055 This message type is used to clear the output that is visible on the frontend.
1056 1056
1057 1057 Message type: ``clear_output``::
1058 1058
1059 1059 content = {
1060 1060
1061 1061 # Wait to clear the output until new output is available. Clears the
1062 1062 # existing output immediately before the new output is displayed.
1063 1063 # Useful for creating simple animations with minimal flickering.
1064 1064 'wait' : bool,
1065 1065 }
1066 1066
1067 1067 .. versionchanged:: 4.1
1068 1068
1069 1069 ``stdout``, ``stderr``, and ``display`` boolean keys for selective clearing are removed,
1070 1070 and ``wait`` is added.
1071 1071 The selective clearing keys are ignored in v4 and the default behavior remains the same,
1072 1072 so v4 clear_output messages will be safely handled by a v4.1 frontend.
1073 1073
1074 1074
1075 1075 Messages on the stdin ROUTER/DEALER sockets
1076 1076 ===========================================
1077 1077
1078 1078 This is a socket where the request/reply pattern goes in the opposite direction:
1079 1079 from the kernel to a *single* frontend, and its purpose is to allow
1080 1080 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
1081 1081 to be fulfilled by the client. The request should be made to the frontend that
1082 1082 made the execution request that prompted ``raw_input`` to be called. For now we
1083 1083 will keep these messages as simple as possible, since they only mean to convey
1084 1084 the ``raw_input(prompt)`` call.
1085 1085
1086 1086 Message type: ``input_request``::
1087 1087
1088 1088 content = {
1089 1089 # the text to show at the prompt
1090 1090 'prompt' : str,
1091 1091 # Is the request for a password?
1092 1092 # If so, the frontend shouldn't echo input.
1093 1093 'password' : bool
1094 1094 }
1095 1095
1096 1096 Message type: ``input_reply``::
1097 1097
1098 1098 content = { 'value' : str }
1099 1099
1100 1100
1101 1101 When ``password`` is True, the frontend should not echo the input as it is entered.
1102 1102
1103 1103 .. versionchanged:: 5.0
1104 1104
1105 1105 ``password`` key added.
1106 1106
1107 1107 .. note::
1108 1108
1109 1109 The stdin socket of the client is required to have the same zmq IDENTITY
1110 1110 as the client's shell socket.
1111 1111 Because of this, the ``input_request`` must be sent with the same IDENTITY
1112 1112 routing prefix as the ``execute_reply`` in order for the frontend to receive
1113 1113 the message.
1114 1114
1115 1115 .. note::
1116 1116
1117 1117 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
1118 1118 practice the kernel should behave like an interactive program. When a
1119 1119 program is opened on the console, the keyboard effectively takes over the
1120 1120 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
1121 1121 Since the IPython kernel effectively behaves like a console program (albeit
1122 1122 one whose "keyboard" is actually living in a separate process and
1123 1123 transported over the zmq connection), raw ``stdin`` isn't expected to be
1124 1124 available.
1125 1125
1126 1126 .. _kernel_heartbeat:
1127 1127
1128 1128 Heartbeat for kernels
1129 1129 =====================
1130 1130
1131 1131 Clients send ping messages on a REQ socket, which are echoed right back
1132 1132 from the Kernel's REP socket. These are simple bytestrings, not full JSON messages described above.
1133 1133
1134 1134
1135 1135 Custom Messages
1136 1136 ===============
1137 1137
1138 1138 .. versionadded:: 4.1
1139 1139
1140 1140 IPython 2.0 (msgspec v4.1) adds a messaging system for developers to add their own objects with Frontend
1141 1141 and Kernel-side components, and allow them to communicate with each other.
1142 1142 To do this, IPython adds a notion of a ``Comm``, which exists on both sides,
1143 1143 and can communicate in either direction.
1144 1144
1145 1145 These messages are fully symmetrical - both the Kernel and the Frontend can send each message,
1146 1146 and no messages expect a reply.
1147 1147 The Kernel listens for these messages on the Shell channel,
1148 1148 and the Frontend listens for them on the IOPub channel.
1149 1149
1150 1150 Opening a Comm
1151 1151 --------------
1152 1152
1153 1153 Opening a Comm produces a ``comm_open`` message, to be sent to the other side::
1154 1154
1155 1155 {
1156 1156 'comm_id' : 'u-u-i-d',
1157 1157 'target_name' : 'my_comm',
1158 1158 'data' : {}
1159 1159 }
1160 1160
1161 1161 Every Comm has an ID and a target name.
1162 1162 The code handling the message on the receiving side is responsible for maintaining a mapping
1163 1163 of target_name keys to constructors.
1164 1164 After a ``comm_open`` message has been sent,
1165 1165 there should be a corresponding Comm instance on both sides.
1166 1166 The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm.
1167 1167
1168 1168 If the ``target_name`` key is not found on the receiving side,
1169 1169 then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state.
1170 1170
1171 1171 Comm Messages
1172 1172 -------------
1173 1173
1174 1174 Comm messages are one-way communications to update comm state,
1175 1175 used for synchronizing widget state, or simply requesting actions of a comm's counterpart.
1176 1176
1177 1177 Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict.
1178 1178
1179 1179 There are no expected replies (of course, one side can send another ``comm_msg`` in reply).
1180 1180
1181 1181 Message type: ``comm_msg``::
1182 1182
1183 1183 {
1184 1184 'comm_id' : 'u-u-i-d',
1185 1185 'data' : {}
1186 1186 }
1187 1187
1188 1188 Tearing Down Comms
1189 1189 ------------------
1190 1190
1191 1191 Since comms live on both sides, when a comm is destroyed the other side must be notified.
1192 1192 This is done with a ``comm_close`` message.
1193 1193
1194 1194 Message type: ``comm_close``::
1195 1195
1196 1196 {
1197 1197 'comm_id' : 'u-u-i-d',
1198 1198 'data' : {}
1199 1199 }
1200 1200
1201 1201 Output Side Effects
1202 1202 -------------------
1203 1203
1204 1204 Since comm messages can execute arbitrary user code,
1205 1205 handlers should set the parent header and publish status busy / idle,
1206 1206 just like an execute request.
1207 1207
1208 1208
1209 1209 To Do
1210 1210 =====
1211 1211
1212 1212 Missing things include:
1213 1213
1214 1214 * Important: finish thinking through the payload concept and API.
1215 1215
1216 1216 .. include:: ../links.txt
General Comments 0
You need to be logged in to leave comments. Login now