##// END OF EJS Templates
Clarify generic message spec vs. Python message API in docs
Alex Kramer -
Show More
@@ -1,948 +1,955 b''
1 .. _messaging:
1 .. _messaging:
2
2
3 ======================
3 ======================
4 Messaging in IPython
4 Messaging in IPython
5 ======================
5 ======================
6
6
7
7
8 Introduction
8 Introduction
9 ============
9 ============
10
10
11 This document explains the basic communications design and messaging
11 This document explains the basic communications design and messaging
12 specification for how the various IPython objects interact over a network
12 specification for how the various IPython objects interact over a network
13 transport. The current implementation uses the ZeroMQ_ library for messaging
13 transport. The current implementation uses the ZeroMQ_ library for messaging
14 within and between hosts.
14 within and between hosts.
15
15
16 .. Note::
16 .. Note::
17
17
18 This document should be considered the authoritative description of the
18 This document should be considered the authoritative description of the
19 IPython messaging protocol, and all developers are strongly encouraged to
19 IPython messaging protocol, and all developers are strongly encouraged to
20 keep it updated as the implementation evolves, so that we have a single
20 keep it updated as the implementation evolves, so that we have a single
21 common reference for all protocol details.
21 common reference for all protocol details.
22
22
23 The basic design is explained in the following diagram:
23 The basic design is explained in the following diagram:
24
24
25 .. image:: figs/frontend-kernel.png
25 .. image:: figs/frontend-kernel.png
26 :width: 450px
26 :width: 450px
27 :alt: IPython kernel/frontend messaging architecture.
27 :alt: IPython kernel/frontend messaging architecture.
28 :align: center
28 :align: center
29 :target: ../_images/frontend-kernel.png
29 :target: ../_images/frontend-kernel.png
30
30
31 A single kernel can be simultaneously connected to one or more frontends. The
31 A single kernel can be simultaneously connected to one or more frontends. The
32 kernel has three sockets that serve the following functions:
32 kernel has three sockets that serve the following functions:
33
33
34 1. stdin: this ROUTER socket is connected to all frontends, and it allows
34 1. stdin: this ROUTER socket is connected to all frontends, and it allows
35 the kernel to request input from the active frontend when :func:`raw_input` is called.
35 the kernel to request input from the active frontend when :func:`raw_input` is called.
36 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
36 The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard'
37 for the kernel while this communication is happening (illustrated in the
37 for the kernel while this communication is happening (illustrated in the
38 figure by the black outline around the central keyboard). In practice,
38 figure by the black outline around the central keyboard). In practice,
39 frontends may display such kernel requests using a special input widget or
39 frontends may display such kernel requests using a special input widget or
40 otherwise indicating that the user is to type input for the kernel instead
40 otherwise indicating that the user is to type input for the kernel instead
41 of normal commands in the frontend.
41 of normal commands in the frontend.
42
42
43 2. Shell: this single ROUTER socket allows multiple incoming connections from
43 2. Shell: this single ROUTER socket allows multiple incoming connections from
44 frontends, and this is the socket where requests for code execution, object
44 frontends, and this is the socket where requests for code execution, object
45 information, prompts, etc. are made to the kernel by any frontend. The
45 information, prompts, etc. are made to the kernel by any frontend. The
46 communication on this socket is a sequence of request/reply actions from
46 communication on this socket is a sequence of request/reply actions from
47 each frontend and the kernel.
47 each frontend and the kernel.
48
48
49 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
49 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all
50 side effects (stdout, stderr, etc.) as well as the requests coming from any
50 side effects (stdout, stderr, etc.) as well as the requests coming from any
51 client over the shell socket and its own requests on the stdin socket. There
51 client over the shell socket and its own requests on the stdin socket. There
52 are a number of actions in Python which generate side effects: :func:`print`
52 are a number of actions in Python which generate side effects: :func:`print`
53 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
53 writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in
54 a multi-client scenario, we want all frontends to be able to know what each
54 a multi-client scenario, we want all frontends to be able to know what each
55 other has sent to the kernel (this can be useful in collaborative scenarios,
55 other has sent to the kernel (this can be useful in collaborative scenarios,
56 for example). This socket allows both side effects and the information
56 for example). This socket allows both side effects and the information
57 about communications taking place with one client over the shell channel
57 about communications taking place with one client over the shell channel
58 to be made available to all clients in a uniform manner.
58 to be made available to all clients in a uniform manner.
59
59
60 All messages are tagged with enough information (details below) for clients
60 All messages are tagged with enough information (details below) for clients
61 to know which messages come from their own interaction with the kernel and
61 to know which messages come from their own interaction with the kernel and
62 which ones are from other clients, so they can display each type
62 which ones are from other clients, so they can display each type
63 appropriately.
63 appropriately.
64
64
65 The actual format of the messages allowed on each of these channels is
65 The actual format of the messages allowed on each of these channels is
66 specified below. Messages are dicts of dicts with string keys and values that
66 specified below. Messages are dicts of dicts with string keys and values that
67 are reasonably representable in JSON. Our current implementation uses JSON
67 are reasonably representable in JSON. Our current implementation uses JSON
68 explicitly as its message format, but this shouldn't be considered a permanent
68 explicitly as its message format, but this shouldn't be considered a permanent
69 feature. As we've discovered that JSON has non-trivial performance issues due
69 feature. As we've discovered that JSON has non-trivial performance issues due
70 to excessive copying, we may in the future move to a pure pickle-based raw
70 to excessive copying, we may in the future move to a pure pickle-based raw
71 message format. However, it should be possible to easily convert from the raw
71 message format. However, it should be possible to easily convert from the raw
72 objects to JSON, since we may have non-python clients (e.g. a web frontend).
72 objects to JSON, since we may have non-python clients (e.g. a web frontend).
73 As long as it's easy to make a JSON version of the objects that is a faithful
73 As long as it's easy to make a JSON version of the objects that is a faithful
74 representation of all the data, we can communicate with such clients.
74 representation of all the data, we can communicate with such clients.
75
75
76 .. Note::
76 .. Note::
77
77
78 Not all of these have yet been fully fleshed out, but the key ones are, see
78 Not all of these have yet been fully fleshed out, but the key ones are, see
79 kernel and frontend files for actual implementation details.
79 kernel and frontend files for actual implementation details.
80
80
81
82 Python functional API
83 =====================
84
85 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
86 should develop, at a few key points, functional forms of all the requests that
87 take arguments in this manner and automatically construct the necessary dict
88 for sending.
89
90
91 General Message Format
81 General Message Format
92 ======================
82 ======================
93
83
94 All messages send or received by any IPython process should have the following
84 A message is defined by the following three-dictionary structure::
95 generic structure::
85
96
97 {
86 {
98 # The message header contains a pair of unique identifiers for the
87 # The message header contains a pair of unique identifiers for the
99 # originating session and the actual message id, in addition to the
88 # originating session and the actual message id, in addition to the
100 # username for the process that generated the message. This is useful in
89 # username for the process that generated the message. This is useful in
101 # collaborative settings where multiple users may be interacting with the
90 # collaborative settings where multiple users may be interacting with the
102 # same kernel simultaneously, so that frontends can label the various
91 # same kernel simultaneously, so that frontends can label the various
103 # messages in a meaningful way.
92 # messages in a meaningful way.
104 'header' : {
93 'header' : {
105 'msg_id' : uuid,
94 'msg_id' : uuid,
106 'username' : str,
95 'username' : str,
107 'session' : uuid
96 'session' : uuid
108 # All recognized message type strings are listed below.
97 # All recognized message type strings are listed below.
109 'msg_type' : str,
98 'msg_type' : str,
110 },
99 },
111 # The msg's unique identifier and type are stored in the header, but
112 # are also accessible at the top-level for convenience.
113 'msg_id' : uuid,
114 'msg_type' : str,
115
100
116 # In a chain of messages, the header from the parent is copied so that
101 # In a chain of messages, the header from the parent is copied so that
117 # clients can track where messages come from.
102 # clients can track where messages come from.
118 'parent_header' : dict,
103 'parent_header' : dict,
119
104
120 # The actual content of the message must be a dict, whose structure
105 # The actual content of the message must be a dict, whose structure
121 # depends on the message type.x
106 # depends on the message type.
122 'content' : dict,
107 'content' : dict,
123 }
108 }
124
109
125 For each message type, the actual content will differ and all existing message
110
126 types are specified in what follows of this document.
111 Python functional API
112 =====================
113
114 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
115 should develop, at a few key points, functional forms of all the requests that
116 take arguments in this manner and automatically construct the necessary dict
117 for sending.
118
119 In addition, the Python implementation of the message specification extends
120 messages upon deserialization to the following form for convenience::
121
122 {
123 'header' : dict,
124 # The msg's unique identifier and type are always stored in the header,
125 # but the Python implementation copies them to the top level.
126 'msg_id' : uuid,
127 'msg_type' : str,
128 'parent_header' : dict
129 'content' : dict
130 }
131
132 All messages sent to or received by any IPython process should have this
133 extended structure.
127
134
128
135
129 Messages on the shell ROUTER/DEALER sockets
136 Messages on the shell ROUTER/DEALER sockets
130 ===========================================
137 ===========================================
131
138
132 .. _execute:
139 .. _execute:
133
140
134 Execute
141 Execute
135 -------
142 -------
136
143
137 This message type is used by frontends to ask the kernel to execute code on
144 This message type is used by frontends to ask the kernel to execute code on
138 behalf of the user, in a namespace reserved to the user's variables (and thus
145 behalf of the user, in a namespace reserved to the user's variables (and thus
139 separate from the kernel's own internal code and variables).
146 separate from the kernel's own internal code and variables).
140
147
141 Message type: ``execute_request``::
148 Message type: ``execute_request``::
142
149
143 content = {
150 content = {
144 # Source code to be executed by the kernel, one or more lines.
151 # Source code to be executed by the kernel, one or more lines.
145 'code' : str,
152 'code' : str,
146
153
147 # A boolean flag which, if True, signals the kernel to execute
154 # A boolean flag which, if True, signals the kernel to execute
148 # this code as quietly as possible. This means that the kernel
155 # this code as quietly as possible. This means that the kernel
149 # will compile the code with 'exec' instead of 'single' (so
156 # will compile the code with 'exec' instead of 'single' (so
150 # sys.displayhook will not fire), and will *not*:
157 # sys.displayhook will not fire), and will *not*:
151 # - broadcast exceptions on the PUB socket
158 # - broadcast exceptions on the PUB socket
152 # - do any logging
159 # - do any logging
153 # - populate any history
160 # - populate any history
154 #
161 #
155 # The default is False.
162 # The default is False.
156 'silent' : bool,
163 'silent' : bool,
157
164
158 # A list of variable names from the user's namespace to be retrieved. What
165 # A list of variable names from the user's namespace to be retrieved. What
159 # returns is a JSON string of the variable's repr(), not a python object.
166 # returns is a JSON string of the variable's repr(), not a python object.
160 'user_variables' : list,
167 'user_variables' : list,
161
168
162 # Similarly, a dict mapping names to expressions to be evaluated in the
169 # Similarly, a dict mapping names to expressions to be evaluated in the
163 # user's dict.
170 # user's dict.
164 'user_expressions' : dict,
171 'user_expressions' : dict,
165
172
166 # Some frontends (e.g. the Notebook) do not support stdin requests. If
173 # Some frontends (e.g. the Notebook) do not support stdin requests. If
167 # raw_input is called from code executed from such a frontend, a
174 # raw_input is called from code executed from such a frontend, a
168 # StdinNotImplementedError will be raised.
175 # StdinNotImplementedError will be raised.
169 'allow_stdin' : True,
176 'allow_stdin' : True,
170
177
171 }
178 }
172
179
173 The ``code`` field contains a single string (possibly multiline). The kernel
180 The ``code`` field contains a single string (possibly multiline). The kernel
174 is responsible for splitting this into one or more independent execution blocks
181 is responsible for splitting this into one or more independent execution blocks
175 and deciding whether to compile these in 'single' or 'exec' mode (see below for
182 and deciding whether to compile these in 'single' or 'exec' mode (see below for
176 detailed execution semantics).
183 detailed execution semantics).
177
184
178 The ``user_`` fields deserve a detailed explanation. In the past, IPython had
185 The ``user_`` fields deserve a detailed explanation. In the past, IPython had
179 the notion of a prompt string that allowed arbitrary code to be evaluated, and
186 the notion of a prompt string that allowed arbitrary code to be evaluated, and
180 this was put to good use by many in creating prompts that displayed system
187 this was put to good use by many in creating prompts that displayed system
181 status, path information, and even more esoteric uses like remote instrument
188 status, path information, and even more esoteric uses like remote instrument
182 status aqcuired over the network. But now that IPython has a clean separation
189 status aqcuired over the network. But now that IPython has a clean separation
183 between the kernel and the clients, the kernel has no prompt knowledge; prompts
190 between the kernel and the clients, the kernel has no prompt knowledge; prompts
184 are a frontend-side feature, and it should be even possible for different
191 are a frontend-side feature, and it should be even possible for different
185 frontends to display different prompts while interacting with the same kernel.
192 frontends to display different prompts while interacting with the same kernel.
186
193
187 The kernel now provides the ability to retrieve data from the user's namespace
194 The kernel now provides the ability to retrieve data from the user's namespace
188 after the execution of the main ``code``, thanks to two fields in the
195 after the execution of the main ``code``, thanks to two fields in the
189 ``execute_request`` message:
196 ``execute_request`` message:
190
197
191 - ``user_variables``: If only variables from the user's namespace are needed, a
198 - ``user_variables``: If only variables from the user's namespace are needed, a
192 list of variable names can be passed and a dict with these names as keys and
199 list of variable names can be passed and a dict with these names as keys and
193 their :func:`repr()` as values will be returned.
200 their :func:`repr()` as values will be returned.
194
201
195 - ``user_expressions``: For more complex expressions that require function
202 - ``user_expressions``: For more complex expressions that require function
196 evaluations, a dict can be provided with string keys and arbitrary python
203 evaluations, a dict can be provided with string keys and arbitrary python
197 expressions as values. The return message will contain also a dict with the
204 expressions as values. The return message will contain also a dict with the
198 same keys and the :func:`repr()` of the evaluated expressions as value.
205 same keys and the :func:`repr()` of the evaluated expressions as value.
199
206
200 With this information, frontends can display any status information they wish
207 With this information, frontends can display any status information they wish
201 in the form that best suits each frontend (a status line, a popup, inline for a
208 in the form that best suits each frontend (a status line, a popup, inline for a
202 terminal, etc).
209 terminal, etc).
203
210
204 .. Note::
211 .. Note::
205
212
206 In order to obtain the current execution counter for the purposes of
213 In order to obtain the current execution counter for the purposes of
207 displaying input prompts, frontends simply make an execution request with an
214 displaying input prompts, frontends simply make an execution request with an
208 empty code string and ``silent=True``.
215 empty code string and ``silent=True``.
209
216
210 Execution semantics
217 Execution semantics
211 ~~~~~~~~~~~~~~~~~~~
218 ~~~~~~~~~~~~~~~~~~~
212
219
213 When the silent flag is false, the execution of use code consists of the
220 When the silent flag is false, the execution of use code consists of the
214 following phases (in silent mode, only the ``code`` field is executed):
221 following phases (in silent mode, only the ``code`` field is executed):
215
222
216 1. Run the ``pre_runcode_hook``.
223 1. Run the ``pre_runcode_hook``.
217
224
218 2. Execute the ``code`` field, see below for details.
225 2. Execute the ``code`` field, see below for details.
219
226
220 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are
227 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are
221 computed. This ensures that any error in the latter don't harm the main
228 computed. This ensures that any error in the latter don't harm the main
222 code execution.
229 code execution.
223
230
224 4. Call any method registered with :meth:`register_post_execute`.
231 4. Call any method registered with :meth:`register_post_execute`.
225
232
226 .. warning::
233 .. warning::
227
234
228 The API for running code before/after the main code block is likely to
235 The API for running code before/after the main code block is likely to
229 change soon. Both the ``pre_runcode_hook`` and the
236 change soon. Both the ``pre_runcode_hook`` and the
230 :meth:`register_post_execute` are susceptible to modification, as we find a
237 :meth:`register_post_execute` are susceptible to modification, as we find a
231 consistent model for both.
238 consistent model for both.
232
239
233 To understand how the ``code`` field is executed, one must know that Python
240 To understand how the ``code`` field is executed, one must know that Python
234 code can be compiled in one of three modes (controlled by the ``mode`` argument
241 code can be compiled in one of three modes (controlled by the ``mode`` argument
235 to the :func:`compile` builtin):
242 to the :func:`compile` builtin):
236
243
237 *single*
244 *single*
238 Valid for a single interactive statement (though the source can contain
245 Valid for a single interactive statement (though the source can contain
239 multiple lines, such as a for loop). When compiled in this mode, the
246 multiple lines, such as a for loop). When compiled in this mode, the
240 generated bytecode contains special instructions that trigger the calling of
247 generated bytecode contains special instructions that trigger the calling of
241 :func:`sys.displayhook` for any expression in the block that returns a value.
248 :func:`sys.displayhook` for any expression in the block that returns a value.
242 This means that a single statement can actually produce multiple calls to
249 This means that a single statement can actually produce multiple calls to
243 :func:`sys.displayhook`, if for example it contains a loop where each
250 :func:`sys.displayhook`, if for example it contains a loop where each
244 iteration computes an unassigned expression would generate 10 calls::
251 iteration computes an unassigned expression would generate 10 calls::
245
252
246 for i in range(10):
253 for i in range(10):
247 i**2
254 i**2
248
255
249 *exec*
256 *exec*
250 An arbitrary amount of source code, this is how modules are compiled.
257 An arbitrary amount of source code, this is how modules are compiled.
251 :func:`sys.displayhook` is *never* implicitly called.
258 :func:`sys.displayhook` is *never* implicitly called.
252
259
253 *eval*
260 *eval*
254 A single expression that returns a value. :func:`sys.displayhook` is *never*
261 A single expression that returns a value. :func:`sys.displayhook` is *never*
255 implicitly called.
262 implicitly called.
256
263
257
264
258 The ``code`` field is split into individual blocks each of which is valid for
265 The ``code`` field is split into individual blocks each of which is valid for
259 execution in 'single' mode, and then:
266 execution in 'single' mode, and then:
260
267
261 - If there is only a single block: it is executed in 'single' mode.
268 - If there is only a single block: it is executed in 'single' mode.
262
269
263 - If there is more than one block:
270 - If there is more than one block:
264
271
265 * if the last one is a single line long, run all but the last in 'exec' mode
272 * if the last one is a single line long, run all but the last in 'exec' mode
266 and the very last one in 'single' mode. This makes it easy to type simple
273 and the very last one in 'single' mode. This makes it easy to type simple
267 expressions at the end to see computed values.
274 expressions at the end to see computed values.
268
275
269 * if the last one is no more than two lines long, run all but the last in
276 * if the last one is no more than two lines long, run all but the last in
270 'exec' mode and the very last one in 'single' mode. This makes it easy to
277 'exec' mode and the very last one in 'single' mode. This makes it easy to
271 type simple expressions at the end to see computed values. - otherwise
278 type simple expressions at the end to see computed values. - otherwise
272 (last one is also multiline), run all in 'exec' mode
279 (last one is also multiline), run all in 'exec' mode
273
280
274 * otherwise (last one is also multiline), run all in 'exec' mode as a single
281 * otherwise (last one is also multiline), run all in 'exec' mode as a single
275 unit.
282 unit.
276
283
277 Any error in retrieving the ``user_variables`` or evaluating the
284 Any error in retrieving the ``user_variables`` or evaluating the
278 ``user_expressions`` will result in a simple error message in the return fields
285 ``user_expressions`` will result in a simple error message in the return fields
279 of the form::
286 of the form::
280
287
281 [ERROR] ExceptionType: Exception message
288 [ERROR] ExceptionType: Exception message
282
289
283 The user can simply send the same variable name or expression for evaluation to
290 The user can simply send the same variable name or expression for evaluation to
284 see a regular traceback.
291 see a regular traceback.
285
292
286 Errors in any registered post_execute functions are also reported similarly,
293 Errors in any registered post_execute functions are also reported similarly,
287 and the failing function is removed from the post_execution set so that it does
294 and the failing function is removed from the post_execution set so that it does
288 not continue triggering failures.
295 not continue triggering failures.
289
296
290 Upon completion of the execution request, the kernel *always* sends a reply,
297 Upon completion of the execution request, the kernel *always* sends a reply,
291 with a status code indicating what happened and additional data depending on
298 with a status code indicating what happened and additional data depending on
292 the outcome. See :ref:`below <execution_results>` for the possible return
299 the outcome. See :ref:`below <execution_results>` for the possible return
293 codes and associated data.
300 codes and associated data.
294
301
295
302
296 Execution counter (old prompt number)
303 Execution counter (old prompt number)
297 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298
305
299 The kernel has a single, monotonically increasing counter of all execution
306 The kernel has a single, monotonically increasing counter of all execution
300 requests that are made with ``silent=False``. This counter is used to populate
307 requests that are made with ``silent=False``. This counter is used to populate
301 the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to
308 the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to
302 display it in some form to the user, which will typically (but not necessarily)
309 display it in some form to the user, which will typically (but not necessarily)
303 be done in the prompts. The value of this counter will be returned as the
310 be done in the prompts. The value of this counter will be returned as the
304 ``execution_count`` field of all ``execute_reply`` messages.
311 ``execution_count`` field of all ``execute_reply`` messages.
305
312
306 .. _execution_results:
313 .. _execution_results:
307
314
308 Execution results
315 Execution results
309 ~~~~~~~~~~~~~~~~~
316 ~~~~~~~~~~~~~~~~~
310
317
311 Message type: ``execute_reply``::
318 Message type: ``execute_reply``::
312
319
313 content = {
320 content = {
314 # One of: 'ok' OR 'error' OR 'abort'
321 # One of: 'ok' OR 'error' OR 'abort'
315 'status' : str,
322 'status' : str,
316
323
317 # The global kernel counter that increases by one with each non-silent
324 # The global kernel counter that increases by one with each non-silent
318 # executed request. This will typically be used by clients to display
325 # executed request. This will typically be used by clients to display
319 # prompt numbers to the user. If the request was a silent one, this will
326 # prompt numbers to the user. If the request was a silent one, this will
320 # be the current value of the counter in the kernel.
327 # be the current value of the counter in the kernel.
321 'execution_count' : int,
328 'execution_count' : int,
322 }
329 }
323
330
324 When status is 'ok', the following extra fields are present::
331 When status is 'ok', the following extra fields are present::
325
332
326 {
333 {
327 # 'payload' will be a list of payload dicts.
334 # 'payload' will be a list of payload dicts.
328 # Each execution payload is a dict with string keys that may have been
335 # Each execution payload is a dict with string keys that may have been
329 # produced by the code being executed. It is retrieved by the kernel at
336 # produced by the code being executed. It is retrieved by the kernel at
330 # the end of the execution and sent back to the front end, which can take
337 # the end of the execution and sent back to the front end, which can take
331 # action on it as needed. See main text for further details.
338 # action on it as needed. See main text for further details.
332 'payload' : list(dict),
339 'payload' : list(dict),
333
340
334 # Results for the user_variables and user_expressions.
341 # Results for the user_variables and user_expressions.
335 'user_variables' : dict,
342 'user_variables' : dict,
336 'user_expressions' : dict,
343 'user_expressions' : dict,
337 }
344 }
338
345
339 .. admonition:: Execution payloads
346 .. admonition:: Execution payloads
340
347
341 The notion of an 'execution payload' is different from a return value of a
348 The notion of an 'execution payload' is different from a return value of a
342 given set of code, which normally is just displayed on the pyout stream
349 given set of code, which normally is just displayed on the pyout stream
343 through the PUB socket. The idea of a payload is to allow special types of
350 through the PUB socket. The idea of a payload is to allow special types of
344 code, typically magics, to populate a data container in the IPython kernel
351 code, typically magics, to populate a data container in the IPython kernel
345 that will be shipped back to the caller via this channel. The kernel
352 that will be shipped back to the caller via this channel. The kernel
346 has an API for this in the PayloadManager::
353 has an API for this in the PayloadManager::
347
354
348 ip.payload_manager.write_payload(payload_dict)
355 ip.payload_manager.write_payload(payload_dict)
349
356
350 which appends a dictionary to the list of payloads.
357 which appends a dictionary to the list of payloads.
351
358
352
359
353 When status is 'error', the following extra fields are present::
360 When status is 'error', the following extra fields are present::
354
361
355 {
362 {
356 'ename' : str, # Exception name, as a string
363 'ename' : str, # Exception name, as a string
357 'evalue' : str, # Exception value, as a string
364 'evalue' : str, # Exception value, as a string
358
365
359 # The traceback will contain a list of frames, represented each as a
366 # The traceback will contain a list of frames, represented each as a
360 # string. For now we'll stick to the existing design of ultraTB, which
367 # string. For now we'll stick to the existing design of ultraTB, which
361 # controls exception level of detail statefully. But eventually we'll
368 # controls exception level of detail statefully. But eventually we'll
362 # want to grow into a model where more information is collected and
369 # want to grow into a model where more information is collected and
363 # packed into the traceback object, with clients deciding how little or
370 # packed into the traceback object, with clients deciding how little or
364 # how much of it to unpack. But for now, let's start with a simple list
371 # how much of it to unpack. But for now, let's start with a simple list
365 # of strings, since that requires only minimal changes to ultratb as
372 # of strings, since that requires only minimal changes to ultratb as
366 # written.
373 # written.
367 'traceback' : list,
374 'traceback' : list,
368 }
375 }
369
376
370
377
371 When status is 'abort', there are for now no additional data fields. This
378 When status is 'abort', there are for now no additional data fields. This
372 happens when the kernel was interrupted by a signal.
379 happens when the kernel was interrupted by a signal.
373
380
374 Kernel attribute access
381 Kernel attribute access
375 -----------------------
382 -----------------------
376
383
377 .. warning::
384 .. warning::
378
385
379 This part of the messaging spec is not actually implemented in the kernel
386 This part of the messaging spec is not actually implemented in the kernel
380 yet.
387 yet.
381
388
382 While this protocol does not specify full RPC access to arbitrary methods of
389 While this protocol does not specify full RPC access to arbitrary methods of
383 the kernel object, the kernel does allow read (and in some cases write) access
390 the kernel object, the kernel does allow read (and in some cases write) access
384 to certain attributes.
391 to certain attributes.
385
392
386 The policy for which attributes can be read is: any attribute of the kernel, or
393 The policy for which attributes can be read is: any attribute of the kernel, or
387 its sub-objects, that belongs to a :class:`Configurable` object and has been
394 its sub-objects, that belongs to a :class:`Configurable` object and has been
388 declared at the class-level with Traits validation, is in principle accessible
395 declared at the class-level with Traits validation, is in principle accessible
389 as long as its name does not begin with a leading underscore. The attribute
396 as long as its name does not begin with a leading underscore. The attribute
390 itself will have metadata indicating whether it allows remote read and/or write
397 itself will have metadata indicating whether it allows remote read and/or write
391 access. The message spec follows for attribute read and write requests.
398 access. The message spec follows for attribute read and write requests.
392
399
393 Message type: ``getattr_request``::
400 Message type: ``getattr_request``::
394
401
395 content = {
402 content = {
396 # The (possibly dotted) name of the attribute
403 # The (possibly dotted) name of the attribute
397 'name' : str,
404 'name' : str,
398 }
405 }
399
406
400 When a ``getattr_request`` fails, there are two possible error types:
407 When a ``getattr_request`` fails, there are two possible error types:
401
408
402 - AttributeError: this type of error was raised when trying to access the
409 - AttributeError: this type of error was raised when trying to access the
403 given name by the kernel itself. This means that the attribute likely
410 given name by the kernel itself. This means that the attribute likely
404 doesn't exist.
411 doesn't exist.
405
412
406 - AccessError: the attribute exists but its value is not readable remotely.
413 - AccessError: the attribute exists but its value is not readable remotely.
407
414
408
415
409 Message type: ``getattr_reply``::
416 Message type: ``getattr_reply``::
410
417
411 content = {
418 content = {
412 # One of ['ok', 'AttributeError', 'AccessError'].
419 # One of ['ok', 'AttributeError', 'AccessError'].
413 'status' : str,
420 'status' : str,
414 # If status is 'ok', a JSON object.
421 # If status is 'ok', a JSON object.
415 'value' : object,
422 'value' : object,
416 }
423 }
417
424
418 Message type: ``setattr_request``::
425 Message type: ``setattr_request``::
419
426
420 content = {
427 content = {
421 # The (possibly dotted) name of the attribute
428 # The (possibly dotted) name of the attribute
422 'name' : str,
429 'name' : str,
423
430
424 # A JSON-encoded object, that will be validated by the Traits
431 # A JSON-encoded object, that will be validated by the Traits
425 # information in the kernel
432 # information in the kernel
426 'value' : object,
433 'value' : object,
427 }
434 }
428
435
429 When a ``setattr_request`` fails, there are also two possible error types with
436 When a ``setattr_request`` fails, there are also two possible error types with
430 similar meanings as those of the ``getattr_request`` case, but for writing.
437 similar meanings as those of the ``getattr_request`` case, but for writing.
431
438
432 Message type: ``setattr_reply``::
439 Message type: ``setattr_reply``::
433
440
434 content = {
441 content = {
435 # One of ['ok', 'AttributeError', 'AccessError'].
442 # One of ['ok', 'AttributeError', 'AccessError'].
436 'status' : str,
443 'status' : str,
437 }
444 }
438
445
439
446
440
447
441 Object information
448 Object information
442 ------------------
449 ------------------
443
450
444 One of IPython's most used capabilities is the introspection of Python objects
451 One of IPython's most used capabilities is the introspection of Python objects
445 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
452 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
446 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
453 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
447 enough that it warrants an explicit message type, especially because frontends
454 enough that it warrants an explicit message type, especially because frontends
448 may want to get object information in response to user keystrokes (like Tab or
455 may want to get object information in response to user keystrokes (like Tab or
449 F1) besides from the user explicitly typing code like ``x??``.
456 F1) besides from the user explicitly typing code like ``x??``.
450
457
451 Message type: ``object_info_request``::
458 Message type: ``object_info_request``::
452
459
453 content = {
460 content = {
454 # The (possibly dotted) name of the object to be searched in all
461 # The (possibly dotted) name of the object to be searched in all
455 # relevant namespaces
462 # relevant namespaces
456 'name' : str,
463 'name' : str,
457
464
458 # The level of detail desired. The default (0) is equivalent to typing
465 # The level of detail desired. The default (0) is equivalent to typing
459 # 'x?' at the prompt, 1 is equivalent to 'x??'.
466 # 'x?' at the prompt, 1 is equivalent to 'x??'.
460 'detail_level' : int,
467 'detail_level' : int,
461 }
468 }
462
469
463 The returned information will be a dictionary with keys very similar to the
470 The returned information will be a dictionary with keys very similar to the
464 field names that IPython prints at the terminal.
471 field names that IPython prints at the terminal.
465
472
466 Message type: ``object_info_reply``::
473 Message type: ``object_info_reply``::
467
474
468 content = {
475 content = {
469 # The name the object was requested under
476 # The name the object was requested under
470 'name' : str,
477 'name' : str,
471
478
472 # Boolean flag indicating whether the named object was found or not. If
479 # Boolean flag indicating whether the named object was found or not. If
473 # it's false, all other fields will be empty.
480 # it's false, all other fields will be empty.
474 'found' : bool,
481 'found' : bool,
475
482
476 # Flags for magics and system aliases
483 # Flags for magics and system aliases
477 'ismagic' : bool,
484 'ismagic' : bool,
478 'isalias' : bool,
485 'isalias' : bool,
479
486
480 # The name of the namespace where the object was found ('builtin',
487 # The name of the namespace where the object was found ('builtin',
481 # 'magics', 'alias', 'interactive', etc.)
488 # 'magics', 'alias', 'interactive', etc.)
482 'namespace' : str,
489 'namespace' : str,
483
490
484 # The type name will be type.__name__ for normal Python objects, but it
491 # The type name will be type.__name__ for normal Python objects, but it
485 # can also be a string like 'Magic function' or 'System alias'
492 # can also be a string like 'Magic function' or 'System alias'
486 'type_name' : str,
493 'type_name' : str,
487
494
488 # The string form of the object, possibly truncated for length if
495 # The string form of the object, possibly truncated for length if
489 # detail_level is 0
496 # detail_level is 0
490 'string_form' : str,
497 'string_form' : str,
491
498
492 # For objects with a __class__ attribute this will be set
499 # For objects with a __class__ attribute this will be set
493 'base_class' : str,
500 'base_class' : str,
494
501
495 # For objects with a __len__ attribute this will be set
502 # For objects with a __len__ attribute this will be set
496 'length' : int,
503 'length' : int,
497
504
498 # If the object is a function, class or method whose file we can find,
505 # If the object is a function, class or method whose file we can find,
499 # we give its full path
506 # we give its full path
500 'file' : str,
507 'file' : str,
501
508
502 # For pure Python callable objects, we can reconstruct the object
509 # For pure Python callable objects, we can reconstruct the object
503 # definition line which provides its call signature. For convenience this
510 # definition line which provides its call signature. For convenience this
504 # is returned as a single 'definition' field, but below the raw parts that
511 # is returned as a single 'definition' field, but below the raw parts that
505 # compose it are also returned as the argspec field.
512 # compose it are also returned as the argspec field.
506 'definition' : str,
513 'definition' : str,
507
514
508 # The individual parts that together form the definition string. Clients
515 # The individual parts that together form the definition string. Clients
509 # with rich display capabilities may use this to provide a richer and more
516 # with rich display capabilities may use this to provide a richer and more
510 # precise representation of the definition line (e.g. by highlighting
517 # precise representation of the definition line (e.g. by highlighting
511 # arguments based on the user's cursor position). For non-callable
518 # arguments based on the user's cursor position). For non-callable
512 # objects, this field is empty.
519 # objects, this field is empty.
513 'argspec' : { # The names of all the arguments
520 'argspec' : { # The names of all the arguments
514 args : list,
521 args : list,
515 # The name of the varargs (*args), if any
522 # The name of the varargs (*args), if any
516 varargs : str,
523 varargs : str,
517 # The name of the varkw (**kw), if any
524 # The name of the varkw (**kw), if any
518 varkw : str,
525 varkw : str,
519 # The values (as strings) of all default arguments. Note
526 # The values (as strings) of all default arguments. Note
520 # that these must be matched *in reverse* with the 'args'
527 # that these must be matched *in reverse* with the 'args'
521 # list above, since the first positional args have no default
528 # list above, since the first positional args have no default
522 # value at all.
529 # value at all.
523 defaults : list,
530 defaults : list,
524 },
531 },
525
532
526 # For instances, provide the constructor signature (the definition of
533 # For instances, provide the constructor signature (the definition of
527 # the __init__ method):
534 # the __init__ method):
528 'init_definition' : str,
535 'init_definition' : str,
529
536
530 # Docstrings: for any object (function, method, module, package) with a
537 # Docstrings: for any object (function, method, module, package) with a
531 # docstring, we show it. But in addition, we may provide additional
538 # docstring, we show it. But in addition, we may provide additional
532 # docstrings. For example, for instances we will show the constructor
539 # docstrings. For example, for instances we will show the constructor
533 # and class docstrings as well, if available.
540 # and class docstrings as well, if available.
534 'docstring' : str,
541 'docstring' : str,
535
542
536 # For instances, provide the constructor and class docstrings
543 # For instances, provide the constructor and class docstrings
537 'init_docstring' : str,
544 'init_docstring' : str,
538 'class_docstring' : str,
545 'class_docstring' : str,
539
546
540 # If it's a callable object whose call method has a separate docstring and
547 # If it's a callable object whose call method has a separate docstring and
541 # definition line:
548 # definition line:
542 'call_def' : str,
549 'call_def' : str,
543 'call_docstring' : str,
550 'call_docstring' : str,
544
551
545 # If detail_level was 1, we also try to find the source code that
552 # If detail_level was 1, we also try to find the source code that
546 # defines the object, if possible. The string 'None' will indicate
553 # defines the object, if possible. The string 'None' will indicate
547 # that no source was found.
554 # that no source was found.
548 'source' : str,
555 'source' : str,
549 }
556 }
550 '
557 '
551
558
552 Complete
559 Complete
553 --------
560 --------
554
561
555 Message type: ``complete_request``::
562 Message type: ``complete_request``::
556
563
557 content = {
564 content = {
558 # The text to be completed, such as 'a.is'
565 # The text to be completed, such as 'a.is'
559 'text' : str,
566 'text' : str,
560
567
561 # The full line, such as 'print a.is'. This allows completers to
568 # The full line, such as 'print a.is'. This allows completers to
562 # make decisions that may require information about more than just the
569 # make decisions that may require information about more than just the
563 # current word.
570 # current word.
564 'line' : str,
571 'line' : str,
565
572
566 # The entire block of text where the line is. This may be useful in the
573 # The entire block of text where the line is. This may be useful in the
567 # case of multiline completions where more context may be needed. Note: if
574 # case of multiline completions where more context may be needed. Note: if
568 # in practice this field proves unnecessary, remove it to lighten the
575 # in practice this field proves unnecessary, remove it to lighten the
569 # messages.
576 # messages.
570
577
571 'block' : str,
578 'block' : str,
572
579
573 # The position of the cursor where the user hit 'TAB' on the line.
580 # The position of the cursor where the user hit 'TAB' on the line.
574 'cursor_pos' : int,
581 'cursor_pos' : int,
575 }
582 }
576
583
577 Message type: ``complete_reply``::
584 Message type: ``complete_reply``::
578
585
579 content = {
586 content = {
580 # The list of all matches to the completion request, such as
587 # The list of all matches to the completion request, such as
581 # ['a.isalnum', 'a.isalpha'] for the above example.
588 # ['a.isalnum', 'a.isalpha'] for the above example.
582 'matches' : list
589 'matches' : list
583 }
590 }
584
591
585
592
586 History
593 History
587 -------
594 -------
588
595
589 For clients to explicitly request history from a kernel. The kernel has all
596 For clients to explicitly request history from a kernel. The kernel has all
590 the actual execution history stored in a single location, so clients can
597 the actual execution history stored in a single location, so clients can
591 request it from the kernel when needed.
598 request it from the kernel when needed.
592
599
593 Message type: ``history_request``::
600 Message type: ``history_request``::
594
601
595 content = {
602 content = {
596
603
597 # If True, also return output history in the resulting dict.
604 # If True, also return output history in the resulting dict.
598 'output' : bool,
605 'output' : bool,
599
606
600 # If True, return the raw input history, else the transformed input.
607 # If True, return the raw input history, else the transformed input.
601 'raw' : bool,
608 'raw' : bool,
602
609
603 # So far, this can be 'range', 'tail' or 'search'.
610 # So far, this can be 'range', 'tail' or 'search'.
604 'hist_access_type' : str,
611 'hist_access_type' : str,
605
612
606 # If hist_access_type is 'range', get a range of input cells. session can
613 # If hist_access_type is 'range', get a range of input cells. session can
607 # be a positive session number, or a negative number to count back from
614 # be a positive session number, or a negative number to count back from
608 # the current session.
615 # the current session.
609 'session' : int,
616 'session' : int,
610 # start and stop are line numbers within that session.
617 # start and stop are line numbers within that session.
611 'start' : int,
618 'start' : int,
612 'stop' : int,
619 'stop' : int,
613
620
614 # If hist_access_type is 'tail', get the last n cells.
621 # If hist_access_type is 'tail', get the last n cells.
615 'n' : int,
622 'n' : int,
616
623
617 # If hist_access_type is 'search', get cells matching the specified glob
624 # If hist_access_type is 'search', get cells matching the specified glob
618 # pattern (with * and ? as wildcards).
625 # pattern (with * and ? as wildcards).
619 'pattern' : str,
626 'pattern' : str,
620
627
621 }
628 }
622
629
623 Message type: ``history_reply``::
630 Message type: ``history_reply``::
624
631
625 content = {
632 content = {
626 # A list of 3 tuples, either:
633 # A list of 3 tuples, either:
627 # (session, line_number, input) or
634 # (session, line_number, input) or
628 # (session, line_number, (input, output)),
635 # (session, line_number, (input, output)),
629 # depending on whether output was False or True, respectively.
636 # depending on whether output was False or True, respectively.
630 'history' : list,
637 'history' : list,
631 }
638 }
632
639
633
640
634 Connect
641 Connect
635 -------
642 -------
636
643
637 When a client connects to the request/reply socket of the kernel, it can issue
644 When a client connects to the request/reply socket of the kernel, it can issue
638 a connect request to get basic information about the kernel, such as the ports
645 a connect request to get basic information about the kernel, such as the ports
639 the other ZeroMQ sockets are listening on. This allows clients to only have
646 the other ZeroMQ sockets are listening on. This allows clients to only have
640 to know about a single port (the shell channel) to connect to a kernel.
647 to know about a single port (the shell channel) to connect to a kernel.
641
648
642 Message type: ``connect_request``::
649 Message type: ``connect_request``::
643
650
644 content = {
651 content = {
645 }
652 }
646
653
647 Message type: ``connect_reply``::
654 Message type: ``connect_reply``::
648
655
649 content = {
656 content = {
650 'shell_port' : int # The port the shell ROUTER socket is listening on.
657 'shell_port' : int # The port the shell ROUTER socket is listening on.
651 'iopub_port' : int # The port the PUB socket is listening on.
658 'iopub_port' : int # The port the PUB socket is listening on.
652 'stdin_port' : int # The port the stdin ROUTER socket is listening on.
659 'stdin_port' : int # The port the stdin ROUTER socket is listening on.
653 'hb_port' : int # The port the heartbeat socket is listening on.
660 'hb_port' : int # The port the heartbeat socket is listening on.
654 }
661 }
655
662
656
663
657
664
658 Kernel shutdown
665 Kernel shutdown
659 ---------------
666 ---------------
660
667
661 The clients can request the kernel to shut itself down; this is used in
668 The clients can request the kernel to shut itself down; this is used in
662 multiple cases:
669 multiple cases:
663
670
664 - when the user chooses to close the client application via a menu or window
671 - when the user chooses to close the client application via a menu or window
665 control.
672 control.
666 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
673 - when the user types 'exit' or 'quit' (or their uppercase magic equivalents).
667 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
674 - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the
668 IPythonQt client) to force a kernel restart to get a clean kernel without
675 IPythonQt client) to force a kernel restart to get a clean kernel without
669 losing client-side state like history or inlined figures.
676 losing client-side state like history or inlined figures.
670
677
671 The client sends a shutdown request to the kernel, and once it receives the
678 The client sends a shutdown request to the kernel, and once it receives the
672 reply message (which is otherwise empty), it can assume that the kernel has
679 reply message (which is otherwise empty), it can assume that the kernel has
673 completed shutdown safely.
680 completed shutdown safely.
674
681
675 Upon their own shutdown, client applications will typically execute a last
682 Upon their own shutdown, client applications will typically execute a last
676 minute sanity check and forcefully terminate any kernel that is still alive, to
683 minute sanity check and forcefully terminate any kernel that is still alive, to
677 avoid leaving stray processes in the user's machine.
684 avoid leaving stray processes in the user's machine.
678
685
679 For both shutdown request and reply, there is no actual content that needs to
686 For both shutdown request and reply, there is no actual content that needs to
680 be sent, so the content dict is empty.
687 be sent, so the content dict is empty.
681
688
682 Message type: ``shutdown_request``::
689 Message type: ``shutdown_request``::
683
690
684 content = {
691 content = {
685 'restart' : bool # whether the shutdown is final, or precedes a restart
692 'restart' : bool # whether the shutdown is final, or precedes a restart
686 }
693 }
687
694
688 Message type: ``shutdown_reply``::
695 Message type: ``shutdown_reply``::
689
696
690 content = {
697 content = {
691 'restart' : bool # whether the shutdown is final, or precedes a restart
698 'restart' : bool # whether the shutdown is final, or precedes a restart
692 }
699 }
693
700
694 .. Note::
701 .. Note::
695
702
696 When the clients detect a dead kernel thanks to inactivity on the heartbeat
703 When the clients detect a dead kernel thanks to inactivity on the heartbeat
697 socket, they simply send a forceful process termination signal, since a dead
704 socket, they simply send a forceful process termination signal, since a dead
698 process is unlikely to respond in any useful way to messages.
705 process is unlikely to respond in any useful way to messages.
699
706
700
707
701 Messages on the PUB/SUB socket
708 Messages on the PUB/SUB socket
702 ==============================
709 ==============================
703
710
704 Streams (stdout, stderr, etc)
711 Streams (stdout, stderr, etc)
705 ------------------------------
712 ------------------------------
706
713
707 Message type: ``stream``::
714 Message type: ``stream``::
708
715
709 content = {
716 content = {
710 # The name of the stream is one of 'stdin', 'stdout', 'stderr'
717 # The name of the stream is one of 'stdin', 'stdout', 'stderr'
711 'name' : str,
718 'name' : str,
712
719
713 # The data is an arbitrary string to be written to that stream
720 # The data is an arbitrary string to be written to that stream
714 'data' : str,
721 'data' : str,
715 }
722 }
716
723
717 When a kernel receives a raw_input call, it should also broadcast it on the pub
724 When a kernel receives a raw_input call, it should also broadcast it on the pub
718 socket with the names 'stdin' and 'stdin_reply'. This will allow other clients
725 socket with the names 'stdin' and 'stdin_reply'. This will allow other clients
719 to monitor/display kernel interactions and possibly replay them to their user
726 to monitor/display kernel interactions and possibly replay them to their user
720 or otherwise expose them.
727 or otherwise expose them.
721
728
722 Display Data
729 Display Data
723 ------------
730 ------------
724
731
725 This type of message is used to bring back data that should be diplayed (text,
732 This type of message is used to bring back data that should be diplayed (text,
726 html, svg, etc.) in the frontends. This data is published to all frontends.
733 html, svg, etc.) in the frontends. This data is published to all frontends.
727 Each message can have multiple representations of the data; it is up to the
734 Each message can have multiple representations of the data; it is up to the
728 frontend to decide which to use and how. A single message should contain all
735 frontend to decide which to use and how. A single message should contain all
729 possible representations of the same information. Each representation should
736 possible representations of the same information. Each representation should
730 be a JSON'able data structure, and should be a valid MIME type.
737 be a JSON'able data structure, and should be a valid MIME type.
731
738
732 Some questions remain about this design:
739 Some questions remain about this design:
733
740
734 * Do we use this message type for pyout/displayhook? Probably not, because
741 * Do we use this message type for pyout/displayhook? Probably not, because
735 the displayhook also has to handle the Out prompt display. On the other hand
742 the displayhook also has to handle the Out prompt display. On the other hand
736 we could put that information into the metadata secion.
743 we could put that information into the metadata secion.
737
744
738 Message type: ``display_data``::
745 Message type: ``display_data``::
739
746
740 content = {
747 content = {
741
748
742 # Who create the data
749 # Who create the data
743 'source' : str,
750 'source' : str,
744
751
745 # The data dict contains key/value pairs, where the kids are MIME
752 # The data dict contains key/value pairs, where the kids are MIME
746 # types and the values are the raw data of the representation in that
753 # types and the values are the raw data of the representation in that
747 # format. The data dict must minimally contain the ``text/plain``
754 # format. The data dict must minimally contain the ``text/plain``
748 # MIME type which is used as a backup representation.
755 # MIME type which is used as a backup representation.
749 'data' : dict,
756 'data' : dict,
750
757
751 # Any metadata that describes the data
758 # Any metadata that describes the data
752 'metadata' : dict
759 'metadata' : dict
753 }
760 }
754
761
755 Python inputs
762 Python inputs
756 -------------
763 -------------
757
764
758 These messages are the re-broadcast of the ``execute_request``.
765 These messages are the re-broadcast of the ``execute_request``.
759
766
760 Message type: ``pyin``::
767 Message type: ``pyin``::
761
768
762 content = {
769 content = {
763 'code' : str, # Source code to be executed, one or more lines
770 'code' : str, # Source code to be executed, one or more lines
764
771
765 # The counter for this execution is also provided so that clients can
772 # The counter for this execution is also provided so that clients can
766 # display it, since IPython automatically creates variables called _iN
773 # display it, since IPython automatically creates variables called _iN
767 # (for input prompt In[N]).
774 # (for input prompt In[N]).
768 'execution_count' : int
775 'execution_count' : int
769 }
776 }
770
777
771 Python outputs
778 Python outputs
772 --------------
779 --------------
773
780
774 When Python produces output from code that has been compiled in with the
781 When Python produces output from code that has been compiled in with the
775 'single' flag to :func:`compile`, any expression that produces a value (such as
782 'single' flag to :func:`compile`, any expression that produces a value (such as
776 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
783 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
777 this value whatever it wants. The default behavior of ``sys.displayhook`` in
784 this value whatever it wants. The default behavior of ``sys.displayhook`` in
778 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
785 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
779 the value as long as it is not ``None`` (which isn't printed at all). In our
786 the value as long as it is not ``None`` (which isn't printed at all). In our
780 case, the kernel instantiates as ``sys.displayhook`` an object which has
787 case, the kernel instantiates as ``sys.displayhook`` an object which has
781 similar behavior, but which instead of printing to stdout, broadcasts these
788 similar behavior, but which instead of printing to stdout, broadcasts these
782 values as ``pyout`` messages for clients to display appropriately.
789 values as ``pyout`` messages for clients to display appropriately.
783
790
784 IPython's displayhook can handle multiple simultaneous formats depending on its
791 IPython's displayhook can handle multiple simultaneous formats depending on its
785 configuration. The default pretty-printed repr text is always given with the
792 configuration. The default pretty-printed repr text is always given with the
786 ``data`` entry in this message. Any other formats are provided in the
793 ``data`` entry in this message. Any other formats are provided in the
787 ``extra_formats`` list. Frontends are free to display any or all of these
794 ``extra_formats`` list. Frontends are free to display any or all of these
788 according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID
795 according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID
789 string, a type string, and the data. The ID is unique to the formatter
796 string, a type string, and the data. The ID is unique to the formatter
790 implementation that created the data. Frontends will typically ignore the ID
797 implementation that created the data. Frontends will typically ignore the ID
791 unless if it has requested a particular formatter. The type string tells the
798 unless if it has requested a particular formatter. The type string tells the
792 frontend how to interpret the data. It is often, but not always a MIME type.
799 frontend how to interpret the data. It is often, but not always a MIME type.
793 Frontends should ignore types that it does not understand. The data itself is
800 Frontends should ignore types that it does not understand. The data itself is
794 any JSON object and depends on the format. It is often, but not always a string.
801 any JSON object and depends on the format. It is often, but not always a string.
795
802
796 Message type: ``pyout``::
803 Message type: ``pyout``::
797
804
798 content = {
805 content = {
799
806
800 # The counter for this execution is also provided so that clients can
807 # The counter for this execution is also provided so that clients can
801 # display it, since IPython automatically creates variables called _N
808 # display it, since IPython automatically creates variables called _N
802 # (for prompt N).
809 # (for prompt N).
803 'execution_count' : int,
810 'execution_count' : int,
804
811
805 # The data dict contains key/value pairs, where the kids are MIME
812 # The data dict contains key/value pairs, where the kids are MIME
806 # types and the values are the raw data of the representation in that
813 # types and the values are the raw data of the representation in that
807 # format. The data dict must minimally contain the ``text/plain``
814 # format. The data dict must minimally contain the ``text/plain``
808 # MIME type which is used as a backup representation.
815 # MIME type which is used as a backup representation.
809 'data' : dict,
816 'data' : dict,
810
817
811 }
818 }
812
819
813 Python errors
820 Python errors
814 -------------
821 -------------
815
822
816 When an error occurs during code execution
823 When an error occurs during code execution
817
824
818 Message type: ``pyerr``::
825 Message type: ``pyerr``::
819
826
820 content = {
827 content = {
821 # Similar content to the execute_reply messages for the 'error' case,
828 # Similar content to the execute_reply messages for the 'error' case,
822 # except the 'status' field is omitted.
829 # except the 'status' field is omitted.
823 }
830 }
824
831
825 Kernel status
832 Kernel status
826 -------------
833 -------------
827
834
828 This message type is used by frontends to monitor the status of the kernel.
835 This message type is used by frontends to monitor the status of the kernel.
829
836
830 Message type: ``status``::
837 Message type: ``status``::
831
838
832 content = {
839 content = {
833 # When the kernel starts to execute code, it will enter the 'busy'
840 # When the kernel starts to execute code, it will enter the 'busy'
834 # state and when it finishes, it will enter the 'idle' state.
841 # state and when it finishes, it will enter the 'idle' state.
835 execution_state : ('busy', 'idle')
842 execution_state : ('busy', 'idle')
836 }
843 }
837
844
838 Kernel crashes
845 Kernel crashes
839 --------------
846 --------------
840
847
841 When the kernel has an unexpected exception, caught by the last-resort
848 When the kernel has an unexpected exception, caught by the last-resort
842 sys.excepthook, we should broadcast the crash handler's output before exiting.
849 sys.excepthook, we should broadcast the crash handler's output before exiting.
843 This will allow clients to notice that a kernel died, inform the user and
850 This will allow clients to notice that a kernel died, inform the user and
844 propose further actions.
851 propose further actions.
845
852
846 Message type: ``crash``::
853 Message type: ``crash``::
847
854
848 content = {
855 content = {
849 # Similarly to the 'error' case for execute_reply messages, this will
856 # Similarly to the 'error' case for execute_reply messages, this will
850 # contain ename, etype and traceback fields.
857 # contain ename, etype and traceback fields.
851
858
852 # An additional field with supplementary information such as where to
859 # An additional field with supplementary information such as where to
853 # send the crash message
860 # send the crash message
854 'info' : str,
861 'info' : str,
855 }
862 }
856
863
857
864
858 Future ideas
865 Future ideas
859 ------------
866 ------------
860
867
861 Other potential message types, currently unimplemented, listed below as ideas.
868 Other potential message types, currently unimplemented, listed below as ideas.
862
869
863 Message type: ``file``::
870 Message type: ``file``::
864
871
865 content = {
872 content = {
866 'path' : 'cool.jpg',
873 'path' : 'cool.jpg',
867 'mimetype' : str,
874 'mimetype' : str,
868 'data' : str,
875 'data' : str,
869 }
876 }
870
877
871
878
872 Messages on the stdin ROUTER/DEALER sockets
879 Messages on the stdin ROUTER/DEALER sockets
873 ===========================================
880 ===========================================
874
881
875 This is a socket where the request/reply pattern goes in the opposite direction:
882 This is a socket where the request/reply pattern goes in the opposite direction:
876 from the kernel to a *single* frontend, and its purpose is to allow
883 from the kernel to a *single* frontend, and its purpose is to allow
877 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
884 ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel
878 to be fulfilled by the client. The request should be made to the frontend that
885 to be fulfilled by the client. The request should be made to the frontend that
879 made the execution request that prompted ``raw_input`` to be called. For now we
886 made the execution request that prompted ``raw_input`` to be called. For now we
880 will keep these messages as simple as possible, since they only mean to convey
887 will keep these messages as simple as possible, since they only mean to convey
881 the ``raw_input(prompt)`` call.
888 the ``raw_input(prompt)`` call.
882
889
883 Message type: ``input_request``::
890 Message type: ``input_request``::
884
891
885 content = { 'prompt' : str }
892 content = { 'prompt' : str }
886
893
887 Message type: ``input_reply``::
894 Message type: ``input_reply``::
888
895
889 content = { 'value' : str }
896 content = { 'value' : str }
890
897
891 .. Note::
898 .. Note::
892
899
893 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
900 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
894 practice the kernel should behave like an interactive program. When a
901 practice the kernel should behave like an interactive program. When a
895 program is opened on the console, the keyboard effectively takes over the
902 program is opened on the console, the keyboard effectively takes over the
896 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
903 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
897 Since the IPython kernel effectively behaves like a console program (albeit
904 Since the IPython kernel effectively behaves like a console program (albeit
898 one whose "keyboard" is actually living in a separate process and
905 one whose "keyboard" is actually living in a separate process and
899 transported over the zmq connection), raw ``stdin`` isn't expected to be
906 transported over the zmq connection), raw ``stdin`` isn't expected to be
900 available.
907 available.
901
908
902
909
903 Heartbeat for kernels
910 Heartbeat for kernels
904 =====================
911 =====================
905
912
906 Initially we had considered using messages like those above over ZMQ for a
913 Initially we had considered using messages like those above over ZMQ for a
907 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
914 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
908 alive at all, even if it may be busy executing user code). But this has the
915 alive at all, even if it may be busy executing user code). But this has the
909 problem that if the kernel is locked inside extension code, it wouldn't execute
916 problem that if the kernel is locked inside extension code, it wouldn't execute
910 the python heartbeat code. But it turns out that we can implement a basic
917 the python heartbeat code. But it turns out that we can implement a basic
911 heartbeat with pure ZMQ, without using any Python messaging at all.
918 heartbeat with pure ZMQ, without using any Python messaging at all.
912
919
913 The monitor sends out a single zmq message (right now, it is a str of the
920 The monitor sends out a single zmq message (right now, it is a str of the
914 monitor's lifetime in seconds), and gets the same message right back, prefixed
921 monitor's lifetime in seconds), and gets the same message right back, prefixed
915 with the zmq identity of the DEALER socket in the heartbeat process. This can be
922 with the zmq identity of the DEALER socket in the heartbeat process. This can be
916 a uuid, or even a full message, but there doesn't seem to be a need for packing
923 a uuid, or even a full message, but there doesn't seem to be a need for packing
917 up a message when the sender and receiver are the exact same Python object.
924 up a message when the sender and receiver are the exact same Python object.
918
925
919 The model is this::
926 The model is this::
920
927
921 monitor.send(str(self.lifetime)) # '1.2345678910'
928 monitor.send(str(self.lifetime)) # '1.2345678910'
922
929
923 and the monitor receives some number of messages of the form::
930 and the monitor receives some number of messages of the form::
924
931
925 ['uuid-abcd-dead-beef', '1.2345678910']
932 ['uuid-abcd-dead-beef', '1.2345678910']
926
933
927 where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and
934 where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and
928 the rest is the message sent by the monitor. No Python code ever has any
935 the rest is the message sent by the monitor. No Python code ever has any
929 access to the message between the monitor's send, and the monitor's recv.
936 access to the message between the monitor's send, and the monitor's recv.
930
937
931
938
932 ToDo
939 ToDo
933 ====
940 ====
934
941
935 Missing things include:
942 Missing things include:
936
943
937 * Important: finish thinking through the payload concept and API.
944 * Important: finish thinking through the payload concept and API.
938
945
939 * Important: ensure that we have a good solution for magics like %edit. It's
946 * Important: ensure that we have a good solution for magics like %edit. It's
940 likely that with the payload concept we can build a full solution, but not
947 likely that with the payload concept we can build a full solution, but not
941 100% clear yet.
948 100% clear yet.
942
949
943 * Finishing the details of the heartbeat protocol.
950 * Finishing the details of the heartbeat protocol.
944
951
945 * Signal handling: specify what kind of information kernel should broadcast (or
952 * Signal handling: specify what kind of information kernel should broadcast (or
946 not) when it receives signals.
953 not) when it receives signals.
947
954
948 .. include:: ../links.rst
955 .. include:: ../links.rst
General Comments 0
You need to be logged in to leave comments. Login now