def read_until(self, terminator, timeout=1000000, timeout_is_soft=False): timer = TimeHelper(timeout) if isinstance(terminator, str): terminator = re.compile(re.escape(terminator)) match = None while True: self._check_for_error() match = re.search(terminator, self._read_buffer) if match: break try: data = self._read_queue.get(True, timer.time_left) # print("RR", repr(data), file=sys.stderr) assert len(data) > 0 self._read_buffer.extend(data) except queue.Empty: if timeout_is_soft: break else: raise TimeoutError("Reaction timeout. Bytes read: %s" % self._read_buffer) if match: size = match.end() else: assert timeout_is_soft size = len(self._read_buffer) data = self._read_buffer[:size] del self._read_buffer[:size] return data
def read(self, size, timeout=10, timeout_is_soft=False): if timeout == 0: if timeout_is_soft: return b"" else: raise TimeoutError() timer = TimeHelper(timeout) while len(self._read_buffer) < size: self._check_for_error() try: self._read_buffer.extend( self._read_queue.get(True, timer.time_left)) except queue.Empty: if timeout_is_soft: return b"" else: raise TimeoutError("Reaction timeout. Bytes read: %s" % self._read_buffer) try: data = self._read_buffer[:size] return data finally: del self._read_buffer[:size]