##// END OF EJS Templates
hook up output for comm messages
MinRK -
Show More
@@ -97,35 +97,35 b' var IPython = (function (IPython) {'
97 97 //-----------------------------------------------------------------------
98 98
99 99 var Comm = function (comm_id, target) {
100 this.comm_id = comm_id;
100 this.comm_id = comm_id || new IPython.utils.uuid();
101 101 this.target = target || 'comm';
102 102 this._msg_callback = this._open_callback = this._close_callback = null;
103 103 };
104 104
105 105 // methods for sending messages
106 Comm.prototype.open = function (data) {
106 Comm.prototype.open = function (data, callbacks) {
107 107 var content = {
108 108 comm_id : this.comm_id,
109 109 target : this.target,
110 110 data : data || {},
111 111 };
112 this.kernel.send_shell_message("comm_open", content);
112 return this.kernel.send_shell_message("comm_open", content, callbacks);
113 113 };
114 114
115 Comm.prototype.send = function (data) {
115 Comm.prototype.send = function (data, callbacks) {
116 116 var content = {
117 117 comm_id : this.comm_id,
118 118 data : data || {},
119 119 };
120 return this.kernel.send_shell_message("comm_msg", content);
120 return this.kernel.send_shell_message("comm_msg", content, callbacks);
121 121 };
122 122
123 Comm.prototype.close = function (data) {
123 Comm.prototype.close = function (data, callbacks) {
124 124 var content = {
125 125 comm_id : this.comm_id,
126 126 data : data || {},
127 127 };
128 return this.kernel.send_shell_message("comm_close", content);
128 return this.kernel.send_shell_message("comm_close", content, callbacks);
129 129 };
130 130
131 131 // methods for registering callbacks for incoming messages
@@ -11,6 +11,8 b''
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13
14 import sys
15
14 16 from IPython.config import LoggingConfigurable
15 17 from IPython.core.prompts import LazyEvaluate
16 18 from IPython.core.getipython import get_ipython
@@ -33,6 +35,23 b' def lazy_keys(dikt):'
33 35 return LazyEvaluate(lambda d: list(d.keys()))
34 36
35 37
38 def with_output(method):
39 """method decorator for ensuring output is handled properly in a message handler
40
41 - sets parent header before entering the method
42 - flushes stdout/stderr after
43 """
44 def method_with_output(self, stream, ident, msg):
45 self.shell.set_parent(msg['header'])
46 try:
47 return method(self, stream, ident, msg)
48 finally:
49 sys.stdout.flush()
50 sys.stderr.flush()
51
52 return method_with_output
53
54
36 55 class CommManager(LoggingConfigurable):
37 56 """Manager for Comms in the Kernel"""
38 57
@@ -97,7 +116,7 b' class CommManager(LoggingConfigurable):'
97 116 return comm
98 117
99 118 # Message handlers
100
119 @with_output
101 120 def comm_open(self, stream, ident, msg):
102 121 """Handler for comm_open messages"""
103 122 content = msg['content']
@@ -117,6 +136,7 b' class CommManager(LoggingConfigurable):'
117 136 comm.handle_open(msg)
118 137 self.register_comm(comm)
119 138
139 @with_output
120 140 def comm_msg(self, stream, ident, msg):
121 141 """Handler for comm_msg messages"""
122 142 content = msg['content']
@@ -127,6 +147,7 b' class CommManager(LoggingConfigurable):'
127 147 return
128 148 comm.handle_msg(msg)
129 149
150 @with_output
130 151 def comm_close(self, stream, ident, msg):
131 152 """Handler for comm_close messages"""
132 153 content = msg['content']
General Comments 0
You need to be logged in to leave comments. Login now