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