##// END OF EJS Templates
flush pub_socket in OutStream.flush...
MinRK -
Show More
@@ -1,88 +1,91 b''
1 import sys
1 import sys
2 import time
2 import time
3 from io import StringIO
3 from io import StringIO
4
4
5 from session import extract_header, Message
5 from session import extract_header, Message
6
6
7 from IPython.utils import io
7 from IPython.utils import io
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Globals
10 # Globals
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Stream classes
14 # Stream classes
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 class OutStream(object):
17 class OutStream(object):
18 """A file like object that publishes the stream to a 0MQ PUB socket."""
18 """A file like object that publishes the stream to a 0MQ PUB socket."""
19
19
20 # The time interval between automatic flushes, in seconds.
20 # The time interval between automatic flushes, in seconds.
21 flush_interval = 0.05
21 flush_interval = 0.05
22 topic=None
22 topic=None
23
23
24 def __init__(self, session, pub_socket, name):
24 def __init__(self, session, pub_socket, name):
25 self.session = session
25 self.session = session
26 self.pub_socket = pub_socket
26 self.pub_socket = pub_socket
27 self.name = name
27 self.name = name
28 self.parent_header = {}
28 self.parent_header = {}
29 self._new_buffer()
29 self._new_buffer()
30
30
31 def set_parent(self, parent):
31 def set_parent(self, parent):
32 self.parent_header = extract_header(parent)
32 self.parent_header = extract_header(parent)
33
33
34 def close(self):
34 def close(self):
35 self.pub_socket = None
35 self.pub_socket = None
36
36
37 def flush(self):
37 def flush(self):
38 #io.rprint('>>>flushing output buffer: %s<<<' % self.name) # dbg
38 #io.rprint('>>>flushing output buffer: %s<<<' % self.name) # dbg
39 if self.pub_socket is None:
39 if self.pub_socket is None:
40 raise ValueError(u'I/O operation on closed file')
40 raise ValueError(u'I/O operation on closed file')
41 else:
41 else:
42 data = self._buffer.getvalue()
42 data = self._buffer.getvalue()
43 if data:
43 if data:
44 content = {u'name':self.name, u'data':data}
44 content = {u'name':self.name, u'data':data}
45 msg = self.session.send(self.pub_socket, u'stream', content=content,
45 msg = self.session.send(self.pub_socket, u'stream', content=content,
46 parent=self.parent_header, ident=self.topic)
46 parent=self.parent_header, ident=self.topic)
47
47
48 if hasattr(self.pub_socket, 'flush'):
49 # socket itself has flush (presumably ZMQStream)
50 self.pub_socket.flush()
48 self._buffer.close()
51 self._buffer.close()
49 self._new_buffer()
52 self._new_buffer()
50
53
51 def isatty(self):
54 def isatty(self):
52 return False
55 return False
53
56
54 def next(self):
57 def next(self):
55 raise IOError('Read not supported on a write only stream.')
58 raise IOError('Read not supported on a write only stream.')
56
59
57 def read(self, size=-1):
60 def read(self, size=-1):
58 raise IOError('Read not supported on a write only stream.')
61 raise IOError('Read not supported on a write only stream.')
59
62
60 def readline(self, size=-1):
63 def readline(self, size=-1):
61 raise IOError('Read not supported on a write only stream.')
64 raise IOError('Read not supported on a write only stream.')
62
65
63 def write(self, string):
66 def write(self, string):
64 if self.pub_socket is None:
67 if self.pub_socket is None:
65 raise ValueError('I/O operation on closed file')
68 raise ValueError('I/O operation on closed file')
66 else:
69 else:
67 # Make sure that we're handling unicode
70 # Make sure that we're handling unicode
68 if not isinstance(string, unicode):
71 if not isinstance(string, unicode):
69 enc = sys.stdin.encoding or sys.getdefaultencoding()
72 enc = sys.stdin.encoding or sys.getdefaultencoding()
70 string = string.decode(enc, 'replace')
73 string = string.decode(enc, 'replace')
71
74
72 self._buffer.write(string)
75 self._buffer.write(string)
73 current_time = time.time()
76 current_time = time.time()
74 if self._start <= 0:
77 if self._start <= 0:
75 self._start = current_time
78 self._start = current_time
76 elif current_time - self._start > self.flush_interval:
79 elif current_time - self._start > self.flush_interval:
77 self.flush()
80 self.flush()
78
81
79 def writelines(self, sequence):
82 def writelines(self, sequence):
80 if self.pub_socket is None:
83 if self.pub_socket is None:
81 raise ValueError('I/O operation on closed file')
84 raise ValueError('I/O operation on closed file')
82 else:
85 else:
83 for string in sequence:
86 for string in sequence:
84 self.write(string)
87 self.write(string)
85
88
86 def _new_buffer(self):
89 def _new_buffer(self):
87 self._buffer = StringIO()
90 self._buffer = StringIO()
88 self._start = -1
91 self._start = -1
General Comments 0
You need to be logged in to leave comments. Login now