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