Merge pull request #1002 from jasongrout/delayevaluation

Implement delayed evaluation for the cell executions happening before a kernel is set up
Min RK 10 years ago
commit 3c4f6e9e74

@ -301,24 +301,21 @@ define([
* @method execute
*/
CodeCell.prototype.execute = function (stop_on_error) {
if (!this.kernel || !this.kernel.is_connected()) {
console.log("Can't execute, kernel is not connected.");
if (!this.kernel) {
console.log("Can't execute cell since kernel is not set.");
return;
}
this.output_area.clear_output(false, true);
if (stop_on_error === undefined) {
stop_on_error = true;
}
this.output_area.clear_output(false, true);
var old_msg_id = this.last_msg_id;
if (old_msg_id) {
this.kernel.clear_callbacks_for_msg(old_msg_id);
if (old_msg_id) {
delete CodeCell.msg_cells[old_msg_id];
}
delete CodeCell.msg_cells[old_msg_id];
this.last_msg_id = null;
}
if (this.get_text().trim().length === 0) {
// nothing to do

@ -62,6 +62,8 @@ define([
this._autorestart_attempt = 0;
this._reconnect_attempt = 0;
this.reconnect_limit = 7;
this._pending_messages = [];
};
/**
@ -408,6 +410,15 @@ define([
* @function _kernel_connected
*/
this.events.trigger('kernel_connected.Kernel', {kernel: this});
// Send pending messages. We shift the message off the queue
// after the message is sent so that if there is an exception,
// the message is still pending.
while (this._pending_messages.length > 0) {
this.ws.send(this._pending_messages[0]);
this._pending_messages.shift();
}
// get kernel info so we know what state the kernel is in
var that = this;
this.kernel_info(function (reply) {
@ -601,18 +612,33 @@ define([
return (this.ws === null);
};
Kernel.prototype._send = function(msg) {
/**
* Send a message (if the kernel is connected) or queue the message for future delivery
*
* Pending messages will automatically be sent when a kernel becomes connected.
*
* @function _send
* @param msg
*/
if (this.is_connected()) {
this.ws.send(msg);
} else {
this._pending_messages.push(msg);
}
}
Kernel.prototype.send_shell_message = function (msg_type, content, callbacks, metadata, buffers) {
/**
* Send a message on the Kernel's shell channel
*
* If the kernel is not connected, the message will be buffered.
*
* @function send_shell_message
*/
if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
var msg = this._get_msg(msg_type, content, metadata, buffers);
msg.channel = 'shell';
this.ws.send(serialize.serialize(msg));
this._send(serialize.serialize(msg));
this.set_callbacks_for_msg(msg.header.msg_id, callbacks);
return msg.header.msg_id;
};
@ -776,16 +802,13 @@ define([
* @function send_input_reply
*/
Kernel.prototype.send_input_reply = function (input) {
if (!this.is_connected()) {
throw new Error("kernel is not connected");
}
var content = {
value : input
};
this.events.trigger('input_reply.Kernel', {kernel: this, content: content});
var msg = this._get_msg("input_reply", content);
msg.channel = 'stdin';
this.ws.send(serialize.serialize(msg));
this._send(serialize.serialize(msg));
return msg.header.msg_id;
};

@ -0,0 +1,56 @@
//
// Test buffering for execution requests.
//
casper.notebook_test(function () {
this.then(function() {
// make sure there are at least three cells for the tests below.
this.append_cell();
this.append_cell();
this.append_cell();
})
this.thenEvaluate(function () {
IPython.notebook.kernel.stop_channels();
var cell = IPython.notebook.get_cell(0);
cell.set_text('a=10; print(a)');
IPython.notebook.execute_cells([0]);
IPython.notebook.kernel.reconnect(1);
});
this.wait_for_output(0);
this.then(function () {
var result = this.get_output_cell(0);
this.test.assertEquals(result.text, '10\n', 'kernels buffer messages if connection is down');
});
this.thenEvaluate(function () {
var cell = IPython.notebook.get_cell(0);
var cellplus = IPython.notebook.get_cell(1);
var cellprint = IPython.notebook.get_cell(2);
cell.set_text('k=1');
cellplus.set_text('k+=1');
cellprint.set_text('k*=2')
IPython.notebook.kernel.stop_channels();
// Repeated execution of cell queued up in the kernel executes
// each execution request in order.
IPython.notebook.execute_cells([0]);
IPython.notebook.execute_cells([2]);
IPython.notebook.execute_cells([1]);
IPython.notebook.execute_cells([1]);
IPython.notebook.execute_cells([1]);
cellprint.set_text('print(k)')
IPython.notebook.execute_cells([2]);
IPython.notebook.kernel.reconnect(1);
});
this.wait_for_output(2);
this.then(function () {
var result = this.get_output_cell(2);
this.test.assertEquals(result.text, '5\n', 'kernels send buffered messages in order');
});
});
Loading…
Cancel
Save