##// END OF EJS Templates
Merge pull request #5804 from minrk/invert-callback-clear...
Thomas Kluyver -
r16606:c94b615f merge
parent child Browse files
Show More
@@ -1,624 +1,624 b''
1 1 // Copyright (c) IPython Development Team.
2 2 // Distributed under the terms of the Modified BSD License.
3 3
4 4 //============================================================================
5 5 // Kernel
6 6 //============================================================================
7 7
8 8 /**
9 9 * @module IPython
10 10 * @namespace IPython
11 11 * @submodule Kernel
12 12 */
13 13
14 14 var IPython = (function (IPython) {
15 15 "use strict";
16 16
17 17 var utils = IPython.utils;
18 18
19 19 // Initialization and connection.
20 20 /**
21 21 * A Kernel Class to communicate with the Python kernel
22 22 * @Class Kernel
23 23 */
24 24 var Kernel = function (kernel_service_url) {
25 25 this.kernel_id = null;
26 26 this.shell_channel = null;
27 27 this.iopub_channel = null;
28 28 this.stdin_channel = null;
29 29 this.kernel_service_url = kernel_service_url;
30 30 this.running = false;
31 31 this.username = "username";
32 32 this.session_id = utils.uuid();
33 33 this._msg_callbacks = {};
34 34 this.post = $.post;
35 35
36 36 if (typeof(WebSocket) !== 'undefined') {
37 37 this.WebSocket = WebSocket;
38 38 } else if (typeof(MozWebSocket) !== 'undefined') {
39 39 this.WebSocket = MozWebSocket;
40 40 } else {
41 41 alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox β‰₯ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.');
42 42 }
43 43
44 44 this.bind_events();
45 45 this.init_iopub_handlers();
46 46 this.comm_manager = new IPython.CommManager(this);
47 47 this.widget_manager = new IPython.WidgetManager(this.comm_manager);
48 48
49 49 this.last_msg_id = null;
50 50 this.last_msg_callbacks = {};
51 51 };
52 52
53 53
54 54 Kernel.prototype._get_msg = function (msg_type, content, metadata) {
55 55 var msg = {
56 56 header : {
57 57 msg_id : utils.uuid(),
58 58 username : this.username,
59 59 session : this.session_id,
60 60 msg_type : msg_type
61 61 },
62 62 metadata : metadata || {},
63 63 content : content,
64 64 parent_header : {}
65 65 };
66 66 return msg;
67 67 };
68 68
69 69 Kernel.prototype.bind_events = function () {
70 70 var that = this;
71 71 $([IPython.events]).on('send_input_reply.Kernel', function(evt, data) {
72 72 that.send_input_reply(data);
73 73 });
74 74 };
75 75
76 76 // Initialize the iopub handlers
77 77
78 78 Kernel.prototype.init_iopub_handlers = function () {
79 79 var output_types = ['stream', 'display_data', 'pyout', 'pyerr'];
80 80 this._iopub_handlers = {};
81 81 this.register_iopub_handler('status', $.proxy(this._handle_status_message, this));
82 82 this.register_iopub_handler('clear_output', $.proxy(this._handle_clear_output, this));
83 83
84 84 for (var i=0; i < output_types.length; i++) {
85 85 this.register_iopub_handler(output_types[i], $.proxy(this._handle_output_message, this));
86 86 }
87 87 };
88 88
89 89 /**
90 90 * Start the Python kernel
91 91 * @method start
92 92 */
93 93 Kernel.prototype.start = function (params) {
94 94 params = params || {};
95 95 if (!this.running) {
96 96 var qs = $.param(params);
97 97 this.post(utils.url_join_encode(this.kernel_service_url) + '?' + qs,
98 98 $.proxy(this._kernel_started, this),
99 99 'json'
100 100 );
101 101 }
102 102 };
103 103
104 104 /**
105 105 * Restart the python kernel.
106 106 *
107 107 * Emit a 'status_restarting.Kernel' event with
108 108 * the current object as parameter
109 109 *
110 110 * @method restart
111 111 */
112 112 Kernel.prototype.restart = function () {
113 113 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
114 114 if (this.running) {
115 115 this.stop_channels();
116 116 this.post(utils.url_join_encode(this.kernel_url, "restart"),
117 117 $.proxy(this._kernel_started, this),
118 118 'json'
119 119 );
120 120 }
121 121 };
122 122
123 123
124 124 Kernel.prototype._kernel_started = function (json) {
125 125 console.log("Kernel started: ", json.id);
126 126 this.running = true;
127 127 this.kernel_id = json.id;
128 128 // trailing 's' in https will become wss for secure web sockets
129 129 this.ws_host = location.protocol.replace('http', 'ws') + "//" + location.host;
130 130 this.kernel_url = utils.url_path_join(this.kernel_service_url, this.kernel_id);
131 131 this.start_channels();
132 132 };
133 133
134 134
135 135 Kernel.prototype._websocket_closed = function(ws_url, early) {
136 136 this.stop_channels();
137 137 $([IPython.events]).trigger('websocket_closed.Kernel',
138 138 {ws_url: ws_url, kernel: this, early: early}
139 139 );
140 140 };
141 141
142 142 /**
143 143 * Start the `shell`and `iopub` channels.
144 144 * Will stop and restart them if they already exist.
145 145 *
146 146 * @method start_channels
147 147 */
148 148 Kernel.prototype.start_channels = function () {
149 149 var that = this;
150 150 this.stop_channels();
151 151 var ws_host_url = this.ws_host + this.kernel_url;
152 152 console.log("Starting WebSockets:", ws_host_url);
153 153 this.shell_channel = new this.WebSocket(
154 154 this.ws_host + utils.url_join_encode(this.kernel_url, "shell")
155 155 );
156 156 this.stdin_channel = new this.WebSocket(
157 157 this.ws_host + utils.url_join_encode(this.kernel_url, "stdin")
158 158 );
159 159 this.iopub_channel = new this.WebSocket(
160 160 this.ws_host + utils.url_join_encode(this.kernel_url, "iopub")
161 161 );
162 162
163 163 var already_called_onclose = false; // only alert once
164 164 var ws_closed_early = function(evt){
165 165 if (already_called_onclose){
166 166 return;
167 167 }
168 168 already_called_onclose = true;
169 169 if ( ! evt.wasClean ){
170 170 that._websocket_closed(ws_host_url, true);
171 171 }
172 172 };
173 173 var ws_closed_late = function(evt){
174 174 if (already_called_onclose){
175 175 return;
176 176 }
177 177 already_called_onclose = true;
178 178 if ( ! evt.wasClean ){
179 179 that._websocket_closed(ws_host_url, false);
180 180 }
181 181 };
182 182 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
183 183 for (var i=0; i < channels.length; i++) {
184 184 channels[i].onopen = $.proxy(this._ws_opened, this);
185 185 channels[i].onclose = ws_closed_early;
186 186 }
187 187 // switch from early-close to late-close message after 1s
188 188 setTimeout(function() {
189 189 for (var i=0; i < channels.length; i++) {
190 190 if (channels[i] !== null) {
191 191 channels[i].onclose = ws_closed_late;
192 192 }
193 193 }
194 194 }, 1000);
195 195 this.shell_channel.onmessage = $.proxy(this._handle_shell_reply, this);
196 196 this.iopub_channel.onmessage = $.proxy(this._handle_iopub_message, this);
197 197 this.stdin_channel.onmessage = $.proxy(this._handle_input_request, this);
198 198 };
199 199
200 200 /**
201 201 * Handle a websocket entering the open state
202 202 * sends session and cookie authentication info as first message.
203 203 * Once all sockets are open, signal the Kernel.status_started event.
204 204 * @method _ws_opened
205 205 */
206 206 Kernel.prototype._ws_opened = function (evt) {
207 207 // send the session id so the Session object Python-side
208 208 // has the same identity
209 209 evt.target.send(this.session_id + ':' + document.cookie);
210 210
211 211 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
212 212 for (var i=0; i < channels.length; i++) {
213 213 // if any channel is not ready, don't trigger event.
214 214 if ( !channels[i].readyState ) return;
215 215 }
216 216 // all events ready, trigger started event.
217 217 $([IPython.events]).trigger('status_started.Kernel', {kernel: this});
218 218 };
219 219
220 220 /**
221 221 * Stop the websocket channels.
222 222 * @method stop_channels
223 223 */
224 224 Kernel.prototype.stop_channels = function () {
225 225 var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
226 226 for (var i=0; i < channels.length; i++) {
227 227 if ( channels[i] !== null ) {
228 228 channels[i].onclose = null;
229 229 channels[i].close();
230 230 }
231 231 }
232 232 this.shell_channel = this.iopub_channel = this.stdin_channel = null;
233 233 };
234 234
235 235 // Main public methods.
236 236
237 237 // send a message on the Kernel's shell channel
238 238 Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata) {
239 239 var msg = this._get_msg(msg_type, content, metadata);
240 240 this.shell_channel.send(JSON.stringify(msg));
241 241 this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
242 242 return msg.header.msg_id;
243 243 };
244 244
245 245 /**
246 246 * Get kernel info
247 247 *
248 248 * @param callback {function}
249 249 * @method object_info
250 250 *
251 251 * When calling this method, pass a callback function that expects one argument.
252 252 * The callback will be passed the complete `kernel_info_reply` message documented
253 253 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#kernel-info)
254 254 */
255 255 Kernel.prototype.kernel_info = function (callback) {
256 256 var callbacks;
257 257 if (callback) {
258 258 callbacks = { shell : { reply : callback } };
259 259 }
260 260 return this.send_shell_message("kernel_info_request", {}, callbacks);
261 261 };
262 262
263 263 /**
264 264 * Get info on an object
265 265 *
266 266 * @param objname {string}
267 267 * @param callback {function}
268 268 * @method object_info
269 269 *
270 270 * When calling this method, pass a callback function that expects one argument.
271 271 * The callback will be passed the complete `object_info_reply` message documented
272 272 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#object-information)
273 273 */
274 274 Kernel.prototype.object_info = function (objname, callback) {
275 275 var callbacks;
276 276 if (callback) {
277 277 callbacks = { shell : { reply : callback } };
278 278 }
279 279
280 280 if (typeof(objname) !== null && objname !== null) {
281 281 var content = {
282 282 oname : objname.toString(),
283 283 detail_level : 0,
284 284 };
285 285 return this.send_shell_message("object_info_request", content, callbacks);
286 286 }
287 287 return;
288 288 };
289 289
290 290 /**
291 291 * Execute given code into kernel, and pass result to callback.
292 292 *
293 293 * @async
294 294 * @method execute
295 295 * @param {string} code
296 296 * @param [callbacks] {Object} With the following keys (all optional)
297 297 * @param callbacks.shell.reply {function}
298 298 * @param callbacks.shell.payload.[payload_name] {function}
299 299 * @param callbacks.iopub.output {function}
300 300 * @param callbacks.iopub.clear_output {function}
301 301 * @param callbacks.input {function}
302 302 * @param {object} [options]
303 303 * @param [options.silent=false] {Boolean}
304 304 * @param [options.user_expressions=empty_dict] {Dict}
305 305 * @param [options.user_variables=empty_list] {List od Strings}
306 306 * @param [options.allow_stdin=false] {Boolean} true|false
307 307 *
308 308 * @example
309 309 *
310 310 * The options object should contain the options for the execute call. Its default
311 311 * values are:
312 312 *
313 313 * options = {
314 314 * silent : true,
315 315 * user_variables : [],
316 316 * user_expressions : {},
317 317 * allow_stdin : false
318 318 * }
319 319 *
320 320 * When calling this method pass a callbacks structure of the form:
321 321 *
322 322 * callbacks = {
323 323 * shell : {
324 324 * reply : execute_reply_callback,
325 325 * payload : {
326 326 * set_next_input : set_next_input_callback,
327 327 * }
328 328 * },
329 329 * iopub : {
330 330 * output : output_callback,
331 331 * clear_output : clear_output_callback,
332 332 * },
333 333 * input : raw_input_callback
334 334 * }
335 335 *
336 336 * Each callback will be passed the entire message as a single arugment.
337 337 * Payload handlers will be passed the corresponding payload and the execute_reply message.
338 338 */
339 339 Kernel.prototype.execute = function (code, callbacks, options) {
340 340
341 341 var content = {
342 342 code : code,
343 343 silent : true,
344 344 store_history : false,
345 345 user_variables : [],
346 346 user_expressions : {},
347 347 allow_stdin : false
348 348 };
349 349 callbacks = callbacks || {};
350 350 if (callbacks.input !== undefined) {
351 351 content.allow_stdin = true;
352 352 }
353 353 $.extend(true, content, options);
354 354 $([IPython.events]).trigger('execution_request.Kernel', {kernel: this, content:content});
355 355 return this.send_shell_message("execute_request", content, callbacks);
356 356 };
357 357
358 358 /**
359 359 * When calling this method, pass a function to be called with the `complete_reply` message
360 360 * as its only argument when it arrives.
361 361 *
362 362 * `complete_reply` is documented
363 363 * [here](http://ipython.org/ipython-doc/dev/development/messaging.html#complete)
364 364 *
365 365 * @method complete
366 366 * @param line {integer}
367 367 * @param cursor_pos {integer}
368 368 * @param callback {function}
369 369 *
370 370 */
371 371 Kernel.prototype.complete = function (line, cursor_pos, callback) {
372 372 var callbacks;
373 373 if (callback) {
374 374 callbacks = { shell : { reply : callback } };
375 375 }
376 376 var content = {
377 377 text : '',
378 378 line : line,
379 379 block : null,
380 380 cursor_pos : cursor_pos
381 381 };
382 382 return this.send_shell_message("complete_request", content, callbacks);
383 383 };
384 384
385 385
386 386 Kernel.prototype.interrupt = function () {
387 387 if (this.running) {
388 388 $([IPython.events]).trigger('status_interrupting.Kernel', {kernel: this});
389 389 this.post(utils.url_join_encode(this.kernel_url, "interrupt"));
390 390 }
391 391 };
392 392
393 393
394 394 Kernel.prototype.kill = function () {
395 395 if (this.running) {
396 396 this.running = false;
397 397 var settings = {
398 398 cache : false,
399 399 type : "DELETE",
400 400 error : utils.log_ajax_error,
401 401 };
402 402 $.ajax(utils.url_join_encode(this.kernel_url), settings);
403 403 }
404 404 };
405 405
406 406 Kernel.prototype.send_input_reply = function (input) {
407 407 var content = {
408 408 value : input,
409 409 };
410 410 $([IPython.events]).trigger('input_reply.Kernel', {kernel: this, content:content});
411 411 var msg = this._get_msg("input_reply", content);
412 412 this.stdin_channel.send(JSON.stringify(msg));
413 413 return msg.header.msg_id;
414 414 };
415 415
416 416
417 417 // Reply handlers
418 418
419 419 Kernel.prototype.register_iopub_handler = function (msg_type, callback) {
420 420 this._iopub_handlers[msg_type] = callback;
421 421 };
422 422
423 423 Kernel.prototype.get_iopub_handler = function (msg_type) {
424 424 // get iopub handler for a specific message type
425 425 return this._iopub_handlers[msg_type];
426 426 };
427 427
428 428
429 429 Kernel.prototype.get_callbacks_for_msg = function (msg_id) {
430 430 // get callbacks for a specific message
431 431 if (msg_id == this.last_msg_id) {
432 432 return this.last_msg_callbacks;
433 433 } else {
434 434 return this._msg_callbacks[msg_id];
435 435 }
436 436 };
437 437
438 438
439 439 Kernel.prototype.clear_callbacks_for_msg = function (msg_id) {
440 440 if (this._msg_callbacks[msg_id] !== undefined ) {
441 441 delete this._msg_callbacks[msg_id];
442 442 }
443 443 };
444 444
445 445 Kernel.prototype._finish_shell = function (msg_id) {
446 446 var callbacks = this._msg_callbacks[msg_id];
447 447 if (callbacks !== undefined) {
448 448 callbacks.shell_done = true;
449 449 if (callbacks.iopub_done) {
450 450 this.clear_callbacks_for_msg(msg_id);
451 451 }
452 452 }
453 453 };
454 454
455 455 Kernel.prototype._finish_iopub = function (msg_id) {
456 456 var callbacks = this._msg_callbacks[msg_id];
457 457 if (callbacks !== undefined) {
458 458 callbacks.iopub_done = true;
459 if (!callbacks.shell_done) {
459 if (callbacks.shell_done) {
460 460 this.clear_callbacks_for_msg(msg_id);
461 461 }
462 462 }
463 463 };
464 464
465 465 /* Set callbacks for a particular message.
466 466 * Callbacks should be a struct of the following form:
467 467 * shell : {
468 468 *
469 469 * }
470 470
471 471 */
472 472 Kernel.prototype.set_callbacks_for_msg = function (msg_id, callbacks) {
473 473 this.last_msg_id = msg_id;
474 474 if (callbacks) {
475 475 // shallow-copy mapping, because we will modify it at the top level
476 476 var cbcopy = this._msg_callbacks[msg_id] = this.last_msg_callbacks = {};
477 477 cbcopy.shell = callbacks.shell;
478 478 cbcopy.iopub = callbacks.iopub;
479 479 cbcopy.input = callbacks.input;
480 480 cbcopy.shell_done = (!callbacks.shell);
481 481 cbcopy.iopub_done = (!callbacks.iopub);
482 482 } else {
483 483 this.last_msg_callbacks = {};
484 484 }
485 485 };
486 486
487 487
488 488 Kernel.prototype._handle_shell_reply = function (e) {
489 489 var reply = $.parseJSON(e.data);
490 490 $([IPython.events]).trigger('shell_reply.Kernel', {kernel: this, reply:reply});
491 491 var content = reply.content;
492 492 var metadata = reply.metadata;
493 493 var parent_id = reply.parent_header.msg_id;
494 494 var callbacks = this.get_callbacks_for_msg(parent_id);
495 495 if (!callbacks || !callbacks.shell) {
496 496 return;
497 497 }
498 498 var shell_callbacks = callbacks.shell;
499 499
500 500 // signal that shell callbacks are done
501 501 this._finish_shell(parent_id);
502 502
503 503 if (shell_callbacks.reply !== undefined) {
504 504 shell_callbacks.reply(reply);
505 505 }
506 506 if (content.payload && shell_callbacks.payload) {
507 507 this._handle_payloads(content.payload, shell_callbacks.payload, reply);
508 508 }
509 509 };
510 510
511 511
512 512 Kernel.prototype._handle_payloads = function (payloads, payload_callbacks, msg) {
513 513 var l = payloads.length;
514 514 // Payloads are handled by triggering events because we don't want the Kernel
515 515 // to depend on the Notebook or Pager classes.
516 516 for (var i=0; i<l; i++) {
517 517 var payload = payloads[i];
518 518 var callback = payload_callbacks[payload.source];
519 519 if (callback) {
520 520 callback(payload, msg);
521 521 }
522 522 }
523 523 };
524 524
525 525 Kernel.prototype._handle_status_message = function (msg) {
526 526 var execution_state = msg.content.execution_state;
527 527 var parent_id = msg.parent_header.msg_id;
528 528
529 529 // dispatch status msg callbacks, if any
530 530 var callbacks = this.get_callbacks_for_msg(parent_id);
531 531 if (callbacks && callbacks.iopub && callbacks.iopub.status) {
532 532 try {
533 533 callbacks.iopub.status(msg);
534 534 } catch (e) {
535 535 console.log("Exception in status msg handler", e, e.stack);
536 536 }
537 537 }
538 538
539 539 if (execution_state === 'busy') {
540 540 $([IPython.events]).trigger('status_busy.Kernel', {kernel: this});
541 541 } else if (execution_state === 'idle') {
542 542 // signal that iopub callbacks are (probably) done
543 543 // async output may still arrive,
544 544 // but only for the most recent request
545 545 this._finish_iopub(parent_id);
546 546
547 547 // trigger status_idle event
548 548 $([IPython.events]).trigger('status_idle.Kernel', {kernel: this});
549 549 } else if (execution_state === 'restarting') {
550 550 // autorestarting is distinct from restarting,
551 551 // in that it means the kernel died and the server is restarting it.
552 552 // status_restarting sets the notification widget,
553 553 // autorestart shows the more prominent dialog.
554 554 $([IPython.events]).trigger('status_autorestarting.Kernel', {kernel: this});
555 555 $([IPython.events]).trigger('status_restarting.Kernel', {kernel: this});
556 556 } else if (execution_state === 'dead') {
557 557 this.stop_channels();
558 558 $([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
559 559 }
560 560 };
561 561
562 562
563 563 // handle clear_output message
564 564 Kernel.prototype._handle_clear_output = function (msg) {
565 565 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
566 566 if (!callbacks || !callbacks.iopub) {
567 567 return;
568 568 }
569 569 var callback = callbacks.iopub.clear_output;
570 570 if (callback) {
571 571 callback(msg);
572 572 }
573 573 };
574 574
575 575
576 576 // handle an output message (pyout, display_data, etc.)
577 577 Kernel.prototype._handle_output_message = function (msg) {
578 578 var callbacks = this.get_callbacks_for_msg(msg.parent_header.msg_id);
579 579 if (!callbacks || !callbacks.iopub) {
580 580 return;
581 581 }
582 582 var callback = callbacks.iopub.output;
583 583 if (callback) {
584 584 callback(msg);
585 585 }
586 586 };
587 587
588 588 // dispatch IOPub messages to respective handlers.
589 589 // each message type should have a handler.
590 590 Kernel.prototype._handle_iopub_message = function (e) {
591 591 var msg = $.parseJSON(e.data);
592 592
593 593 var handler = this.get_iopub_handler(msg.header.msg_type);
594 594 if (handler !== undefined) {
595 595 handler(msg);
596 596 }
597 597 };
598 598
599 599
600 600 Kernel.prototype._handle_input_request = function (e) {
601 601 var request = $.parseJSON(e.data);
602 602 var header = request.header;
603 603 var content = request.content;
604 604 var metadata = request.metadata;
605 605 var msg_type = header.msg_type;
606 606 if (msg_type !== 'input_request') {
607 607 console.log("Invalid input request!", request);
608 608 return;
609 609 }
610 610 var callbacks = this.get_callbacks_for_msg(request.parent_header.msg_id);
611 611 if (callbacks) {
612 612 if (callbacks.input) {
613 613 callbacks.input(request);
614 614 }
615 615 }
616 616 };
617 617
618 618
619 619 IPython.Kernel = Kernel;
620 620
621 621 return IPython;
622 622
623 623 }(IPython));
624 624
General Comments 0
You need to be logged in to leave comments. Login now