def _send(self, dataType, args=None): import struct stream = BytesIO() Pickler(stream).dump((dataType, args)) raw_data = stream.getvalue() self.pipe_c2p.write(struct.pack("<i", len(raw_data))) self.pipe_c2p.write(raw_data) self.pipe_c2p.flush()
def _send(self, data_type, args=None): """ :param str data_type: :param object args: """ assert data_type is not None import struct stream = BytesIO() Pickler(stream).dump((data_type, args)) raw_data = stream.getvalue() assert len(raw_data) > 0 self.pipe_c2p.write(struct.pack("<i", len(raw_data))) self.pipe_c2p.write(raw_data) self.pipe_c2p.flush()
def _read_next_raw(self): """ :return: (data_type, args) :rtype: (str, object) """ import struct size_raw = self.pipe_c2p[0].read(4) if len(size_raw) < 4: raise EOFError size, = struct.unpack("<i", size_raw) assert size > 0, "%s: We expect to get some non-empty package. Invalid Python mod in Sprint?" % (self,) stream = BytesIO() read_size = 0 while read_size < size: data_raw = self.pipe_c2p[0].read(size - read_size) if len(data_raw) == 0: raise EOFError("%s: expected to read %i bytes but got EOF after %i bytes" % (self, size, read_size)) read_size += len(data_raw) stream.write(data_raw) stream.seek(0) try: if PY3: # encoding is for converting Python2 strings to Python3. # Cannot use utf8 because Numpy will also encode the data as strings and there we need it as bytes. data_type, args = Unpickler(stream, encoding="bytes").load() else: data_type, args = Unpickler(stream).load() except EOFError: raise Exception("%s: parse error of %i bytes (%r)" % (self, size, stream.getvalue())) return data_type, args
def _read_next_raw(self): import struct size_raw = self.pipe_c2p[0].read(4) if len(size_raw) < 4: raise EOFError size, = struct.unpack("<i", size_raw) stream = BytesIO() read_size = 0 while read_size < size: data_raw = self.pipe_c2p[0].read(size - read_size) if len(data_raw) == 0: raise EOFError read_size += len(data_raw) stream.write(data_raw) stream.seek(0) if PY3: # encoding is for converting Python2 strings to Python3. # Cannot use utf8 because Numpy will also encode the data as strings and there we need it as bytes. dataType, args = Unpickler(stream, encoding="bytes").load() else: dataType, args = Unpickler(stream).load() return dataType, args