##// END OF EJS Templates
Update message spec with details about attribute, prompts, etc....
Fernando Perez -
Show More
@@ -1,587 +1,668 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:: frontend-kernel.png
25 .. image:: 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. REQ: this socket is connected to a *single* frontend at a time, and it allows
34 1. REQ: this socket is connected to a *single* frontend at a time, and it allows
35 the kernel to request input from a frontend when :func:`raw_input` is called.
35 the kernel to request input from a frontend when :func:`raw_input` is called.
36 The frontend holding the matching REP socket acts as a 'virtual keyboard'
36 The frontend holding the matching REP socket 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. XREP: this single sockets allows multiple incoming connections from
43 2. XREP: this single sockets 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. PUB: this socket is the 'broadcast channel' where the kernel publishes all
49 3. PUB: 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 XREP socket and its own requests on the REP socket. There
51 client over the XREP socket and its own requests on the REP 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 XREQ/XREP channel
57 about communications taking place with one client over the XREQ/XREP 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
81
82 Python functional API
82 Python functional API
83 =====================
83 =====================
84
84
85 As messages are dicts, they map naturally to a ``func(**kw)`` call form. We
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
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
87 take arguments in this manner and automatically construct the necessary dict
88 for sending.
88 for sending.
89
89
90
90
91 General Message Format
91 General Message Format
92 ======================
92 ======================
93
93
94 All messages send or received by any IPython process should have the following
94 All messages send or received by any IPython process should have the following
95 generic structure::
95 generic structure::
96
96
97 {
97 {
98 # The message header contains a pair of unique identifiers for the
98 # The message header contains a pair of unique identifiers for the
99 # originating session and the actual message id, in addition to the
99 # originating session and the actual message id, in addition to the
100 # username for the process that generated the message. This is useful in
100 # username for the process that generated the message. This is useful in
101 # collaborative settings where multiple users may be interacting with the
101 # collaborative settings where multiple users may be interacting with the
102 # same kernel simultaneously, so that frontends can label the various
102 # same kernel simultaneously, so that frontends can label the various
103 # messages in a meaningful way.
103 # messages in a meaningful way.
104 'header' : { 'msg_id' : uuid,
104 'header' : { 'msg_id' : uuid,
105 'username' : str,
105 'username' : str,
106 'session' : uuid
106 'session' : uuid
107 },
107 },
108
108
109 # In a chain of messages, the header from the parent is copied so that
109 # In a chain of messages, the header from the parent is copied so that
110 # clients can track where messages come from.
110 # clients can track where messages come from.
111 'parent_header' : dict,
111 'parent_header' : dict,
112
112
113 # All recognized message type strings are listed below.
113 # All recognized message type strings are listed below.
114 'msg_type' : str,
114 'msg_type' : str,
115
115
116 # The actual content of the message must be a dict, whose structure
116 # The actual content of the message must be a dict, whose structure
117 # depends on the message type.x
117 # depends on the message type.x
118 'content' : dict,
118 'content' : dict,
119 }
119 }
120
120
121 For each message type, the actual content will differ and all existing message
121 For each message type, the actual content will differ and all existing message
122 types are specified in what follows of this document.
122 types are specified in what follows of this document.
123
123
124
124
125 Messages on the XREP/XREQ socket
125 Messages on the XREP/XREQ socket
126 ================================
126 ================================
127
127
128 .. _execute:
128 .. _execute:
129
129
130 Execute
130 Execute
131 -------
131 -------
132
132
133 The execution request contains a single string, but this may be a multiline
133 The execution request contains a single string, but this may be a multiline
134 string. The kernel is responsible for splitting this into possibly more than
134 string. The kernel is responsible for splitting this into possibly more than
135 one block and deciding whether to compile these in 'single' or 'exec' mode.
135 one block and deciding whether to compile these in 'single' or 'exec' mode.
136 We're still sorting out this policy. The current inputsplitter is capable of
136 We're still sorting out this policy. The current inputsplitter is capable of
137 splitting the input for blocks that can all be run as 'single', but in the long
137 splitting the input for blocks that can all be run as 'single', but in the long
138 run it may prove cleaner to only use 'single' mode for truly single-line
138 run it may prove cleaner to only use 'single' mode for truly single-line
139 inputs, and run all multiline input in 'exec' mode. This would preserve the
139 inputs, and run all multiline input in 'exec' mode. This would preserve the
140 natural behavior of single-line inputs while allowing long cells to behave more
140 natural behavior of single-line inputs while allowing long cells to behave more
141 likea a script. This design will be refined as we complete the implementation.
141 likea a script. This design will be refined as we complete the implementation.
142
142
143 .. Note::
144
145 What today we call 'prompt requests' will be encoded in the
146 ``state_template`` field.
147
143 Message type: ``execute_request``::
148 Message type: ``execute_request``::
144
149
145 content = {
150 content = {
146 # 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.
147 'code' : str,
152 'code' : str,
148
153
149 # A boolean flag which, if True, signals the kernel to execute this
154 # A boolean flag which, if True, signals the kernel to execute this
150 # code as quietly as possible. This means that the kernel will compile
155 # code as quietly as possible. This means that the kernel will compile
151 # the code with 'exec' instead of 'single' (so sys.displayhook will not
156 # the code witIPython/core/tests/h 'exec' instead of 'single' (so
152 # fire), and will *not*:
157 # sys.displayhook will not fire), and will *not*:
153 # - broadcast exceptions on the PUB socket
158 # - broadcast exceptions on the PUB socket
154 # - do any logging
159 # - do any logging
155 # - populate any history
160 # - populate any history
161 #
156 # The default is False.
162 # The default is False.
157 'silent' : bool,
163 'silent' : bool,
164
165 # An optional string to request arbitrary state information from the
166 # kernel. This string is evaluated via the itpl module, and it can
167 # therefore contain arbitrary code for execution.
168
169 'state_template' : str,
158 }
170 }
159
171
160 Upon execution, the kernel *always* sends a reply, with a status code
172 Execution semantics
161 indicating what happened and additional data depending on the outcome.
173 Upon execution of the ``code`` field, the kernel *always* sends a reply,
174 with a status code indicating what happened and additional data depending
175 on the outcome.
176
177 Any code in the ``state_template`` string is evaluated, but full exceptions
178 that may occur are *not* propagated back. If any error occurs during the
179 evaluation, the value of the string will simply be::
180
181 [ERROR in <contents of template>: ExceptionType - Exception message]
182
183 The user can simply send the same code contained in the template for normal
184 evaluation to see a regular traceback.
185
186 Execution counter (old prompt number)
187 The kernel has a single, monotonically increasing counter of all execution
188 requests that are made with ``silent=False``. This counter is used to
189 populate the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will
190 likely want to display it in some form to the user, which will typically
191 (but not necessarily) be done in the prompts. The value of this counter
192 will be returned as the ``execution_count`` field of all ``execute_reply```
193 messages.
162
194
163 Message type: ``execute_reply``::
195 Message type: ``execute_reply``::
164
196
165 content = {
197 content = {
166 # One of: 'ok' OR 'error' OR 'abort'
198 # One of: 'ok' OR 'error' OR 'abort'
167 'status' : str,
199 'status' : str,
168
200
169 # This has the same structure as the output of a prompt request, but is
201 # The global kernel counter that increases by one with each non-silent
170 # for the client to set up the *next* prompt (with identical limitations
202 # executed request. This will typically be used by clients to display
171 # to a prompt request)
203 # prompt numbers to the user. If the request was a silent one, this will
172 'next_prompt' : {
204 # be the current value of the counter in the kernel.
173 'prompt_string' : str,
205 'execution_count' : int,
174 'prompt_number' : int,
206
175 'input_sep' : str
207 # If the state_template was provided, this will contain the evaluated
176 },
208 # form of the template.
177
209 'state' : str,
178 # The prompt number of the actual execution for this code, which may be
179 # different from the one used when the code was typed, which was the
180 # 'next_prompt' field of the *previous* request. They will differ in the
181 # case where there is more than one client talking simultaneously to a
182 # kernel, since the numbers can go out of sync. GUI clients can use this
183 # to correct the previously written number in-place, terminal ones may
184 # re-print a corrected one if desired.
185 'prompt_number' : int,
186 }
210 }
187
211
188 When status is 'ok', the following extra fields are present::
212 When status is 'ok', the following extra fields are present::
189
213
190 {
214 {
191 # The kernel will often transform the input provided to it. This
215 # The kernel will often transform the input provided to it. If the
192 # contains the transformed code, which is what was actually executed.
216 # '---->' transform had been applied, this is filled, otherwise it's the
217 # empty string. So transformations like magics don't appear here, only
218 # autocall ones.
219
193 'transformed_code' : str,
220 'transformed_code' : str,
194
221
195 # The execution payload is a dict with string keys that may have been
222 # The execution payload is a dict with string keys that may have been
196 # produced by the code being executed. It is retrieved by the kernel at
223 # produced by the code being executed. It is retrieved by the kernel at
197 # the end of the execution and sent back to the front end, which can take
224 # the end of the execution and sent back to the front end, which can take
198 # action on it as needed. See main text for further details.
225 # action on it as needed. See main text for further details.
199 'payload' : dict,
226 'payload' : dict,
200 }
227 }
201
228
202 .. admonition:: Execution payloads
229 .. admonition:: Execution payloads
203
230
204 The notion of an 'execution payload' is different from a return value of a
231 The notion of an 'execution payload' is different from a return value of a
205 given set of code, which normally is just displayed on the pyout stream
232 given set of code, which normally is just displayed on the pyout stream
206 through the PUB socket. The idea of a payload is to allow special types of
233 through the PUB socket. The idea of a payload is to allow special types of
207 code, typically magics, to populate a data container in the IPython kernel
234 code, typically magics, to populate a data container in the IPython kernel
208 that will be shipped back to the caller via this channel. The kernel will
235 that will be shipped back to the caller via this channel. The kernel will
209 have an API for this, probably something along the lines of::
236 have an API for this, probably something along the lines of::
210
237
211 ip.exec_payload_add(key, value)
238 ip.exec_payload_add(key, value)
212
239
213 though this API is still in the design stages. The data returned in this
240 though this API is still in the design stages. The data returned in this
214 payload will allow frontends to present special views of what just happened.
241 payload will allow frontends to present special views of what just happened.
215
242
216
243
217 When status is 'error', the following extra fields are present::
244 When status is 'error', the following extra fields are present::
218
245
219 {
246 {
220 'exc_name' : str, # Exception name, as a string
247 'exc_name' : str, # Exception name, as a string
221 'exc_value' : str, # Exception value, as a string
248 'exc_value' : str, # Exception value, as a string
222
249
223 # The traceback will contain a list of frames, represented each as a
250 # The traceback will contain a list of frames, represented each as a
224 # string. For now we'll stick to the existing design of ultraTB, which
251 # string. For now we'll stick to the existing design of ultraTB, which
225 # controls exception level of detail statefully. But eventually we'll
252 # controls exception level of detail statefully. But eventually we'll
226 # want to grow into a model where more information is collected and
253 # want to grow into a model where more information is collected and
227 # packed into the traceback object, with clients deciding how little or
254 # packed into the traceback object, with clients deciding how little or
228 # how much of it to unpack. But for now, let's start with a simple list
255 # how much of it to unpack. But for now, let's start with a simple list
229 # of strings, since that requires only minimal changes to ultratb as
256 # of strings, since that requires only minimal changes to ultratb as
230 # written.
257 # written.
231 'traceback' : list,
258 'traceback' : list,
232 }
259 }
233
260
234
261
235 When status is 'abort', there are for now no additional data fields. This
262 When status is 'abort', there are for now no additional data fields. This
236 happens when the kernel was interrupted by a signal.
263 happens when the kernel was interrupted by a signal.
237
264
265 Kernel attribute access
266 -----------------------
238
267
239 Prompt
268 While this protocol does not specify full RPC access to arbitrary methods of
240 ------
269 the kernel object, the kernel does allow read (and in some cases write) access
270 to certain attributes.
241
271
242 A simple request for a current prompt string.
272 The policy for which attributes can be read is: any attribute of the kernel, or
273 its sub-objects, that belongs to a :class:`Configurable` object and has been
274 declared at the class-level with Traits validation, is in principle accessible
275 as long as its name does not begin with a leading underscore. The attribute
276 itself will have metadata indicating whether it allows remote read and/or write
277 access. The message spec follows for attribute read and write requests.
243
278
244 Message type: ``prompt_request``::
279 Message type: ``getattr_request``::
245
280
246 content = {}
281 content = {
282 # The (possibly dotted) name of the attribute
283 'name' : str
284 }
247
285
248 In the reply, the prompt string comes back with the prompt number placeholder
286 When a ``getattr_request`` fails, there are two possible error types:
249 *unevaluated*. The message format is:
287
250
288 - AttributeError: this type of error was raised when trying to access the
251 Message type: ``prompt_reply``::
289 given name by the kernel itself. This means that the attribute likely
290 doesn't exist.
291
292 - AccessError: the attribute exists but its value is not readable remotely.
293
294
295 Message type: ``getattr_reply``::
296
297 content = {
298 # One of ['ok', 'AttributeError', 'AccessError'].
299 'status' : str
300 # If status is 'ok', a JSON object.
301 'value' : object
302 }
303
304 Message type: ``setattr_request``::
252
305
253 content = {
306 content = {
254 'prompt_string' : str,
307 # The (possibly dotted) name of the attribute
255 'prompt_number' : int,
308 'name' : str
256 'input_sep' : str
309
310 # A JSON-encoded object, that will be validated by the Traits
311 # information in the kernel
312 'value' : object
257 }
313 }
258
314
259 Clients can produce a prompt with ``prompt_string.format(prompt_number)``, but
315 When a ``setattr_request`` fails, there are also two possible error types with
260 they should be aware that the actual prompt number for that input could change
316 similar meanings as those of the ``getattr_request`` case, but for writing.
261 later, in the case where multiple clients are interacting with a single
317
262 kernel.
318 Message type: ``setattr_reply``::
319
320 content = {
321 # One of ['ok', 'AttributeError', 'AccessError'].
322 'status' : str
323 }
263
324
264
325
265 Object information
326 Object information
266 ------------------
327 ------------------
267
328
268 One of IPython's most used capabilities is the introspection of Python objects
329 One of IPython's most used capabilities is the introspection of Python objects
269 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
330 in the user's namespace, typically invoked via the ``?`` and ``??`` characters
270 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
331 (which in reality are shorthands for the ``%pinfo`` magic). This is used often
271 enough that it warrants an explicit message type, especially because frontends
332 enough that it warrants an explicit message type, especially because frontends
272 may want to get object information in response to user keystrokes (like Tab or
333 may want to get object information in response to user keystrokes (like Tab or
273 F1) besides from the user explicitly typing code like ``x??``.
334 F1) besides from the user explicitly typing code like ``x??``.
274
335
275 Message type: ``object_info_request``::
336 Message type: ``object_info_request``::
276
337
277 content = {
338 content = {
278 # The (possibly dotted) name of the object to be searched in all
339 # The (possibly dotted) name of the object to be searched in all
279 # relevant namespaces
340 # relevant namespaces
280 'name' : str,
341 'name' : str,
281
342
282 # The level of detail desired. The default (0) is equivalent to typing
343 # The level of detail desired. The default (0) is equivalent to typing
283 # 'x?' at the prompt, 1 is equivalent to 'x??'.
344 # 'x?' at the prompt, 1 is equivalent to 'x??'.
284 'detail_level' : int,
345 'detail_level' : int,
285 }
346 }
286
347
287 The returned information will be a dictionary with keys very similar to the
348 The returned information will be a dictionary with keys very similar to the
288 field names that IPython prints at the terminal.
349 field names that IPython prints at the terminal.
289
350
290 Message type: ``object_info_reply``::
351 Message type: ``object_info_reply``::
291
352
292 content = {
353 content = {
293 # Flags for magics and system aliases
354 # Flags for magics and system aliases
294 'ismagic' : bool,
355 'ismagic' : bool,
295 'isalias' : bool,
356 'isalias' : bool,
296
357
297 # The name of the namespace where the object was found ('builtin',
358 # The name of the namespace where the object was found ('builtin',
298 # 'magics', 'alias', 'interactive', etc.)
359 # 'magics', 'alias', 'interactive', etc.)
299 'namespace' : str,
360 'namespace' : str,
300
361
301 # The type name will be type.__name__ for normal Python objects, but it
362 # The type name will be type.__name__ for normal Python objects, but it
302 # can also be a string like 'Magic function' or 'System alias'
363 # can also be a string like 'Magic function' or 'System alias'
303 'type_name' : str,
364 'type_name' : str,
304
365
305 'string_form' : str,
366 'string_form' : str,
306
367
307 # For objects with a __class__ attribute this will be set
368 # For objects with a __class__ attribute this will be set
308 'base_class' : str,
369 'base_class' : str,
309
370
310 # For objects with a __len__ attribute this will be set
371 # For objects with a __len__ attribute this will be set
311 'length' : int,
372 'length' : int,
312
373
313 # If the object is a function, class or method whose file we can find,
374 # If the object is a function, class or method whose file we can find,
314 # we give its full path
375 # we give its full path
315 'file' : str,
376 'file' : str,
316
377
317 # For pure Python callable objects, we can reconstruct the object
378 # For pure Python callable objects, we can reconstruct the object
318 # definition line which provides its call signature
379 # definition line which provides its call signature. For convenience this
380 # is returned as a single 'definition' field, but below the raw parts that
381 # compose it are also returned as the argspec field.
319 'definition' : str,
382 'definition' : str,
320
383
384 # The individual parts that together form the definition string. Clients
385 # with rich display capabilities may use this to provide a richer and more
386 # precise representation of the definition line (e.g. by highlighting
387 # arguments based on the user's cursor position). For non-callable
388 # objects, this field is empty.
389 'argspec' : { # The names of all the arguments
390 args : list,
391 # The name of the varargs (*args), if any
392 varargs : str,
393 # The name of the varkw (**kw), if any
394 varkw : str
395 # The values (as strings) of all default arguments. Note
396 # that these must be matched *in reverse* with the 'args'
397 # list above, since the first positional args have no default
398 # value at all.
399 func_defaults : list
400 }
401
321 # For instances, provide the constructor signature (the definition of
402 # For instances, provide the constructor signature (the definition of
322 # the __init__ method):
403 # the __init__ method):
323 'init_definition' : str,
404 'init_definition' : str,
324
405
325 # Docstrings: for any object (function, method, module, package) with a
406 # Docstrings: for any object (function, method, module, package) with a
326 # docstring, we show it. But in addition, we may provide additional
407 # docstring, we show it. But in addition, we may provide additional
327 # docstrings. For example, for instances we will show the constructor
408 # docstrings. For example, for instances we will show the constructor
328 # and class docstrings as well, if available.
409 # and class docstrings as well, if available.
329 'docstring' : str,
410 'docstring' : str,
330
411
331 # For instances, provide the constructor and class docstrings
412 # For instances, provide the constructor and class docstrings
332 'init_docstring' : str,
413 'init_docstring' : str,
333 'class_docstring' : str,
414 'class_docstring' : str,
334
415
335 # If detail_level was 1, we also try to find the source code that
416 # If detail_level was 1, we also try to find the source code that
336 # defines the object, if possible. The string 'None' will indicate
417 # defines the object, if possible. The string 'None' will indicate
337 # that no source was found.
418 # that no source was found.
338 'source' : str,
419 'source' : str,
339 }
420 }
340
421
341
422
342 Complete
423 Complete
343 --------
424 --------
344
425
345 Message type: ``complete_request``::
426 Message type: ``complete_request``::
346
427
347 content = {
428 content = {
348 # The text to be completed, such as 'a.is'
429 # The text to be completed, such as 'a.is'
349 'text' : str,
430 'text' : str,
350
431
351 # The full line, such as 'print a.is'. This allows completers to
432 # The full line, such as 'print a.is'. This allows completers to
352 # make decisions that may require information about more than just the
433 # make decisions that may require information about more than just the
353 # current word.
434 # current word.
354 'line' : str,
435 'line' : str,
355
436
356 # The entire block of text where the line is. This may be useful in the
437 # The entire block of text where the line is. This may be useful in the
357 # case of multiline completions where more context may be needed. Note: if
438 # case of multiline completions where more context may be needed. Note: if
358 # in practice this field proves unnecessary, remove it to lighten the
439 # in practice this field proves unnecessary, remove it to lighten the
359 # messages.
440 # messages.
360
441
361 'block' : str,
442 'block' : str,
362
443
363 # The position of the cursor where the user hit 'TAB' on the line.
444 # The position of the cursor where the user hit 'TAB' on the line.
364 'cursor_pos' : int,
445 'cursor_pos' : int,
365 }
446 }
366
447
367 Message type: ``complete_reply``::
448 Message type: ``complete_reply``::
368
449
369 content = {
450 content = {
370 # The list of all matches to the completion request, such as
451 # The list of all matches to the completion request, such as
371 # ['a.isalnum', 'a.isalpha'] for the above example.
452 # ['a.isalnum', 'a.isalpha'] for the above example.
372 'matches' : list
453 'matches' : list
373 }
454 }
374
455
375
456
376 History
457 History
377 -------
458 -------
378
459
379 For clients to explicitly request history from a kernel. The kernel has all
460 For clients to explicitly request history from a kernel. The kernel has all
380 the actual execution history stored in a single location, so clients can
461 the actual execution history stored in a single location, so clients can
381 request it from the kernel when needed.
462 request it from the kernel when needed.
382
463
383 Message type: ``history_request``::
464 Message type: ``history_request``::
384
465
385 content = {
466 content = {
386
467
387 # If True, also return output history in the resulting dict.
468 # If True, also return output history in the resulting dict.
388 'output' : bool,
469 'output' : bool,
389
470
390 # If True, return the raw input history, else the transformed input.
471 # If True, return the raw input history, else the transformed input.
391 'raw' : bool,
472 'raw' : bool,
392
473
393 # This parameter can be one of: A number, a pair of numbers, None
474 # This parameter can be one of: A number, a pair of numbers, None
394 # If not given, last 40 are returned.
475 # If not given, last 40 are returned.
395 # - number n: return the last n entries.
476 # - number n: return the last n entries.
396 # - pair n1, n2: return entries in the range(n1, n2).
477 # - pair n1, n2: return entries in the range(n1, n2).
397 # - None: return all history
478 # - None: return all history
398 'index' : n or (n1, n2) or None,
479 'index' : n or (n1, n2) or None,
399 }
480 }
400
481
401 Message type: ``history_reply``::
482 Message type: ``history_reply``::
402
483
403 content = {
484 content = {
404 # A dict with prompt numbers as keys and either (input, output) or input
485 # A dict with prompt numbers as keys and either (input, output) or input
405 # as the value depending on whether output was True or False,
486 # as the value depending on whether output was True or False,
406 # respectively.
487 # respectively.
407 'history' : dict,
488 'history' : dict,
408 }
489 }
409 Messages on the PUB/SUB socket
490 Messages on the PUB/SUB socket
410 ==============================
491 ==============================
411
492
412 Streams (stdout, stderr, etc)
493 Streams (stdout, stderr, etc)
413 ------------------------------
494 ------------------------------
414
495
415 Message type: ``stream``::
496 Message type: ``stream``::
416
497
417 content = {
498 content = {
418 # The name of the stream is one of 'stdin', 'stdout', 'stderr'
499 # The name of the stream is one of 'stdin', 'stdout', 'stderr'
419 'name' : str,
500 'name' : str,
420
501
421 # The data is an arbitrary string to be written to that stream
502 # The data is an arbitrary string to be written to that stream
422 'data' : str,
503 'data' : str,
423 }
504 }
424
505
425 When a kernel receives a raw_input call, it should also broadcast it on the pub
506 When a kernel receives a raw_input call, it should also broadcast it on the pub
426 socket with the names 'stdin' and 'stdin_reply'. This will allow other clients
507 socket with the names 'stdin' and 'stdin_reply'. This will allow other clients
427 to monitor/display kernel interactions and possibly replay them to their user
508 to monitor/display kernel interactions and possibly replay them to their user
428 or otherwise expose them.
509 or otherwise expose them.
429
510
430 Python inputs
511 Python inputs
431 -------------
512 -------------
432
513
433 These messages are the re-broadcast of the ``execute_request``.
514 These messages are the re-broadcast of the ``execute_request``.
434
515
435 Message type: ``pyin``::
516 Message type: ``pyin``::
436
517
437 content = {
518 content = {
438 # Source code to be executed, one or more lines
519 # Source code to be executed, one or more lines
439 'code' : str
520 'code' : str
440 }
521 }
441
522
442 Python outputs
523 Python outputs
443 --------------
524 --------------
444
525
445 When Python produces output from code that has been compiled in with the
526 When Python produces output from code that has been compiled in with the
446 'single' flag to :func:`compile`, any expression that produces a value (such as
527 'single' flag to :func:`compile`, any expression that produces a value (such as
447 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
528 ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with
448 this value whatever it wants. The default behavior of ``sys.displayhook`` in
529 this value whatever it wants. The default behavior of ``sys.displayhook`` in
449 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
530 the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of
450 the value as long as it is not ``None`` (which isn't printed at all). In our
531 the value as long as it is not ``None`` (which isn't printed at all). In our
451 case, the kernel instantiates as ``sys.displayhook`` an object which has
532 case, the kernel instantiates as ``sys.displayhook`` an object which has
452 similar behavior, but which instead of printing to stdout, broadcasts these
533 similar behavior, but which instead of printing to stdout, broadcasts these
453 values as ``pyout`` messages for clients to display appropriately.
534 values as ``pyout`` messages for clients to display appropriately.
454
535
455 Message type: ``pyout``::
536 Message type: ``pyout``::
456
537
457 content = {
538 content = {
458 # The data is typically the repr() of the object.
539 # The data is typically the repr() of the object.
459 'data' : str,
540 'data' : str,
460
541
461 # The prompt number for this execution is also provided so that clients
542 # The prompt number for this execution is also provided so that clients
462 # can display it, since IPython automatically creates variables called
543 # can display it, since IPython automatically creates variables called
463 # _N (for prompt N).
544 # _N (for prompt N).
464 'prompt_number' : int,
545 'prompt_number' : int,
465 }
546 }
466
547
467 Python errors
548 Python errors
468 -------------
549 -------------
469
550
470 When an error occurs during code execution
551 When an error occurs during code execution
471
552
472 Message type: ``pyerr``::
553 Message type: ``pyerr``::
473
554
474 content = {
555 content = {
475 # Similar content to the execute_reply messages for the 'error' case,
556 # Similar content to the execute_reply messages for the 'error' case,
476 # except the 'status' field is omitted.
557 # except the 'status' field is omitted.
477 }
558 }
478
559
479 Kernel crashes
560 Kernel crashes
480 --------------
561 --------------
481
562
482 When the kernel has an unexpected exception, caught by the last-resort
563 When the kernel has an unexpected exception, caught by the last-resort
483 sys.excepthook, we should broadcast the crash handler's output before exiting.
564 sys.excepthook, we should broadcast the crash handler's output before exiting.
484 This will allow clients to notice that a kernel died, inform the user and
565 This will allow clients to notice that a kernel died, inform the user and
485 propose further actions.
566 propose further actions.
486
567
487 Message type: ``crash``::
568 Message type: ``crash``::
488
569
489 content = {
570 content = {
490 # Similarly to the 'error' case for execute_reply messages, this will
571 # Similarly to the 'error' case for execute_reply messages, this will
491 # contain exc_name, exc_type and traceback fields.
572 # contain exc_name, exc_type and traceback fields.
492
573
493 # An additional field with supplementary information such as where to
574 # An additional field with supplementary information such as where to
494 # send the crash message
575 # send the crash message
495 'info' : str,
576 'info' : str,
496 }
577 }
497
578
498
579
499 Future ideas
580 Future ideas
500 ------------
581 ------------
501
582
502 Other potential message types, currently unimplemented, listed below as ideas.
583 Other potential message types, currently unimplemented, listed below as ideas.
503
584
504 Message type: ``file``::
585 Message type: ``file``::
505
586
506 content = {
587 content = {
507 'path' : 'cool.jpg',
588 'path' : 'cool.jpg',
508 'mimetype' : str,
589 'mimetype' : str,
509 'data' : str,
590 'data' : str,
510 }
591 }
511
592
512
593
513 Messages on the REQ/REP socket
594 Messages on the REQ/REP socket
514 ==============================
595 ==============================
515
596
516 This is a socket that goes in the opposite direction: from the kernel to a
597 This is a socket that goes in the opposite direction: from the kernel to a
517 *single* frontend, and its purpose is to allow ``raw_input`` and similar
598 *single* frontend, and its purpose is to allow ``raw_input`` and similar
518 operations that read from ``sys.stdin`` on the kernel to be fulfilled by the
599 operations that read from ``sys.stdin`` on the kernel to be fulfilled by the
519 client. For now we will keep these messages as simple as possible, since they
600 client. For now we will keep these messages as simple as possible, since they
520 basically only mean to convey the ``raw_input(prompt)`` call.
601 basically only mean to convey the ``raw_input(prompt)`` call.
521
602
522 Message type: ``input_request``::
603 Message type: ``input_request``::
523
604
524 content = { 'prompt' : str }
605 content = { 'prompt' : str }
525
606
526 Message type: ``input_reply``::
607 Message type: ``input_reply``::
527
608
528 content = { 'value' : str }
609 content = { 'value' : str }
529
610
530 .. Note::
611 .. Note::
531
612
532 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
613 We do not explicitly try to forward the raw ``sys.stdin`` object, because in
533 practice the kernel should behave like an interactive program. When a
614 practice the kernel should behave like an interactive program. When a
534 program is opened on the console, the keyboard effectively takes over the
615 program is opened on the console, the keyboard effectively takes over the
535 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
616 ``stdin`` file descriptor, and it can't be used for raw reading anymore.
536 Since the IPython kernel effectively behaves like a console program (albeit
617 Since the IPython kernel effectively behaves like a console program (albeit
537 one whose "keyboard" is actually living in a separate process and
618 one whose "keyboard" is actually living in a separate process and
538 transported over the zmq connection), raw ``stdin`` isn't expected to be
619 transported over the zmq connection), raw ``stdin`` isn't expected to be
539 available.
620 available.
540
621
541
622
542 Heartbeat for kernels
623 Heartbeat for kernels
543 =====================
624 =====================
544
625
545 Initially we had considered using messages like those above over ZMQ for a
626 Initially we had considered using messages like those above over ZMQ for a
546 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
627 kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is
547 alive at all, even if it may be busy executing user code). But this has the
628 alive at all, even if it may be busy executing user code). But this has the
548 problem that if the kernel is locked inside extension code, it wouldn't execute
629 problem that if the kernel is locked inside extension code, it wouldn't execute
549 the python heartbeat code. But it turns out that we can implement a basic
630 the python heartbeat code. But it turns out that we can implement a basic
550 heartbeat with pure ZMQ, without using any Python messaging at all.
631 heartbeat with pure ZMQ, without using any Python messaging at all.
551
632
552 The monitor sends out a single zmq message (right now, it is a str of the
633 The monitor sends out a single zmq message (right now, it is a str of the
553 monitor's lifetime in seconds), and gets the same message right back, prefixed
634 monitor's lifetime in seconds), and gets the same message right back, prefixed
554 with the zmq identity of the XREQ socket in the heartbeat process. This can be
635 with the zmq identity of the XREQ socket in the heartbeat process. This can be
555 a uuid, or even a full message, but there doesn't seem to be a need for packing
636 a uuid, or even a full message, but there doesn't seem to be a need for packing
556 up a message when the sender and receiver are the exact same Python object.
637 up a message when the sender and receiver are the exact same Python object.
557
638
558 The model is this::
639 The model is this::
559
640
560 monitor.send(str(self.lifetime)) # '1.2345678910'
641 monitor.send(str(self.lifetime)) # '1.2345678910'
561
642
562 and the monitor receives some number of messages of the form::
643 and the monitor receives some number of messages of the form::
563
644
564 ['uuid-abcd-dead-beef', '1.2345678910']
645 ['uuid-abcd-dead-beef', '1.2345678910']
565
646
566 where the first part is the zmq.IDENTITY of the heart's XREQ on the engine, and
647 where the first part is the zmq.IDENTITY of the heart's XREQ on the engine, and
567 the rest is the message sent by the monitor. No Python code ever has any
648 the rest is the message sent by the monitor. No Python code ever has any
568 access to the message between the monitor's send, and the monitor's recv.
649 access to the message between the monitor's send, and the monitor's recv.
569
650
570
651
571 ToDo
652 ToDo
572 ====
653 ====
573
654
574 Missing things include:
655 Missing things include:
575
656
576 * Important: finish thinking through the payload concept and API.
657 * Important: finish thinking through the payload concept and API.
577
658
578 * Important: ensure that we have a good solution for magics like %edit. It's
659 * Important: ensure that we have a good solution for magics like %edit. It's
579 likely that with the payload concept we can build a full solution, but not
660 likely that with the payload concept we can build a full solution, but not
580 100% clear yet.
661 100% clear yet.
581
662
582 * Finishing the details of the heartbeat protocol.
663 * Finishing the details of the heartbeat protocol.
583
664
584 * Signal handling: specify what kind of information kernel should broadcast (or
665 * Signal handling: specify what kind of information kernel should broadcast (or
585 not) when it receives signals.
666 not) when it receives signals.
586
667
587 .. include:: ../links.rst
668 .. include:: ../links.rst
General Comments 0
You need to be logged in to leave comments. Login now