diff --git a/IPython/zmq/iostream.py b/IPython/zmq/iostream.py index 8ba2d7a..a893f84 100644 --- a/IPython/zmq/iostream.py +++ b/IPython/zmq/iostream.py @@ -4,6 +4,8 @@ from cStringIO import StringIO from session import extract_header, Message +from IPython.utils import io + #----------------------------------------------------------------------------- # Stream classes #----------------------------------------------------------------------------- @@ -28,6 +30,7 @@ class OutStream(object): self.pub_socket = None def flush(self): + #io.rprint('>>>flushing output buffer: %s<<<' % self.name) # dbg if self.pub_socket is None: raise ValueError(u'I/O operation on closed file') else: @@ -36,7 +39,7 @@ class OutStream(object): content = {u'name':self.name, u'data':data} msg = self.session.msg(u'stream', content=content, parent=self.parent_header) - print>>sys.__stdout__, Message(msg) + io.raw_print(msg) self.pub_socket.send_json(msg) self._buffer.close() @@ -74,4 +77,4 @@ class OutStream(object): def _new_buffer(self): self._buffer = StringIO() - self._start = -1 \ No newline at end of file + self._start = -1 diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index 924aabc..cfcb4b3 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -195,6 +195,15 @@ class Kernel(Configurable): # Send the reply. reply_msg = self.session.msg(u'execute_reply', reply_content, parent) io.raw_print(reply_msg) + + # Flush output before sending the reply. + sys.stdout.flush() + sys.stderr.flush() + # FIXME: on rare occasions, the flush doesn't seem to make it to the + # clients... This seems to mitigate the problem, but we definitely need + # to better understand what's going on. + time.sleep(0.05) + self.reply_socket.send(ident, zmq.SNDMORE) self.reply_socket.send_json(reply_msg) if reply_msg['content']['status'] == u'error':