Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger Kernel.status_started after websockets open #4079

Merged
merged 1 commit into from
Sep 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions IPython/html/static/services/kernels/js/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ var IPython = (function (IPython) {
this.ws_url = ws_url;
this.kernel_url = this.base_url + "/" + this.kernel_id;
this.start_channels();
$([IPython.events]).trigger('status_started.Kernel', {kernel: this});
};


Expand All @@ -144,11 +143,7 @@ var IPython = (function (IPython) {
this.shell_channel = new this.WebSocket(ws_url + "/shell");
this.stdin_channel = new this.WebSocket(ws_url + "/stdin");
this.iopub_channel = new this.WebSocket(ws_url + "/iopub");
send_cookie = function(){
// send the session id so the Session object Python-side
// has the same identity
this.send(that.session_id + ':' + document.cookie);
};

var already_called_onclose = false; // only alert once
var ws_closed_early = function(evt){
if (already_called_onclose){
Expand All @@ -170,7 +165,7 @@ var IPython = (function (IPython) {
};
var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
for (var i=0; i < channels.length; i++) {
channels[i].onopen = send_cookie;
channels[i].onopen = $.proxy(this._ws_opened, this);
channels[i].onclose = ws_closed_early;
}
// switch from early-close to late-close message after 1s
Expand All @@ -187,7 +182,27 @@ var IPython = (function (IPython) {
};

/**
* Stop the `shell`and `iopub` channels.
* Handle a websocket entering the open state
* sends session and cookie authentication info as first message.
* Once all sockets are open, signal the Kernel.status_started event.
* @method _ws_opened
*/
Kernel.prototype._ws_opened = function (evt) {
// send the session id so the Session object Python-side
// has the same identity
evt.target.send(this.session_id + ':' + document.cookie);

var channels = [this.shell_channel, this.iopub_channel, this.stdin_channel];
for (var i=0; i < channels.length; i++) {
// if any channel is not ready, don't trigger event.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious about the logic: if a channel isn't ready and the early exit is forced, is there a chance for this event to fire later when those channels are ready? Should there be a similar event triggered when the channels become ready to ensure that once the last one is ready, the event is always sent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand the question. The event will only be triggered if all three connections are ready. If the early-close event fires before they are all ready, the started event will never fire because the websockets will be closed.

if ( !channels[i].readyState ) return;
}
// all events ready, trigger started event.
$([IPython.events]).trigger('status_started.Kernel', {kernel: this});
};

/**
* Stop the websocket channels.
* @method stop_channels
*/
Kernel.prototype.stop_channels = function () {
Expand Down