##// END OF EJS Templates
Added message throttling
Jonathan Frederic -
Show More
@@ -92,10 +92,13 b' define(["static/components/underscore/underscore-min.js",'
92 92 this.widget_model_types = {};
93 93 this.widget_view_types = {};
94 94 this.model_widget_views = {};
95 this.pending_msgs = 0;
96 this.msg_throttle = 3;
97 this.msg_buffer = {};
95 98
96 99 var that = this;
97 100 Backbone.sync = function(method, model, options, error) {
98 var result = that.send_sync(method, model, options);
101 var result = that.handle_sync(method, model, options);
99 102 if (options.success) {
100 103 options.success(result);
101 104 }
@@ -227,6 +230,24 b' define(["static/components/underscore/underscore-min.js",'
227 230 }
228 231 }
229 232
233 // Handle when a msg status changes in the kernel.
234 WidgetManager.prototype.handle_status = function (msg) {
235 //execution_state : ('busy', 'idle', 'starting')
236 if (msg.content.execution_state=='idle') {
237 // Send buffer if this message caused another message to be
238 // throttled.
239 if (this.msg_throttle == --this.pending_msgs &&
240 this.msg_buffer.length > 0) {
241 var outputarea = this._get_msg_outputarea(msg);
242 var callbacks = this._make_callbacks(outputarea);
243 var data = {sync_method: 'patch', sync_data: this.msg_buffer};
244 comm.send(data, callbacks);
245 this.pending_msgs++;
246 this.msg_buffer = {};
247 }
248 }
249 }
250
230 251 // Get the cell output area corresponding to the comm.
231 252 WidgetManager.prototype._get_comm_outputarea = function (comm) {
232 253 // TODO: get element from comm instead of guessing
@@ -234,25 +255,53 b' define(["static/components/underscore/underscore-min.js",'
234 255 return cell.output_area;
235 256 }
236 257
237 // Send widget state to python backend.
238 WidgetManager.prototype.send_sync = function (method, model, options) {
239 var model_json = model.toJSON();
258 // Get the cell output area corresponding to the msg_id.
259 WidgetManager.prototype._get_msg_outputarea = function (msg) {
260 // TODO: get element from msg_id instead of guessing
261 // msg.parent_header.msg_id
262 var cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index())
263 return cell.output_area;
264 }
240 265
241 // Only send updated state if the state hasn't been changed during an update.
242 if (!this.updating) {
243 // Create a callback for the output if the widget has an output area associate with it.
266 // Build a callback dict.
267 WidgetManager.prototype._make_callbacks = function (outputarea) {
244 268 var callbacks = {};
245 var comm = model.comm;
246 var outputarea = this._get_comm_outputarea(comm);
247 269 if (outputarea != null) {
248 270 callbacks = {
249 271 iopub : {
272 status : $.proxy(this.handle_status, this),
250 273 output : $.proxy(outputarea.handle_output, outputarea),
251 274 clear_output : $.proxy(outputarea.handle_clear_output, outputarea)}
252 275 };
253 };
276 }
277 return callbacks;
278 }
279
280 // Send widget state to python backend.
281 WidgetManager.prototype.handle_sync = function (method, model, options) {
282 var model_json = model.toJSON();
283
284 // Only send updated state if the state hasn't been changed during an update.
285 if (!this.updating) {
286 // Create a callback for the output if the widget has an output area associate with it.
287 var callbacks = this._make_callbacks(this._get_comm_outputarea(comm));
288 var comm = model.comm;
254 289
255 // If this is a patch operation, just send the changes.
290 if (this.pending_msgs >= this.msg_throttle) {
291 // The throttle has been exceeded, buffer the current msg so
292 // it can be sent once the kernel has finished processing
293 // some of the existing messages.
294 if (method=='patch') {
295 for (var attr in options.attrs) {
296 this.msg_buffer[attr] = options.attrs[attr];
297 }
298 } else {
299 this.msg_buffer = $.extend({}, model_json); // Copy
300 }
301 } else {
302 // We haven't exceeded the throttle, send the message like
303 // normal. If this is a patch operation, just send the
304 // changes.
256 305 var send_json = model_json;
257 306 if (method=='patch') {
258 307 send_json = {};
@@ -262,6 +311,8 b' define(["static/components/underscore/underscore-min.js",'
262 311 }
263 312 var data = {sync_method: method, sync_data: send_json};
264 313 comm.send(data, callbacks);
314 this.pending_msgs++;
315 }
265 316 }
266 317
267 318 // Since the comm is a one-way communication, assume the message
General Comments 0
You need to be logged in to leave comments. Login now