def exec_(b: bytes): msg = loads(b) # func = getattr(subprocess, msg.get("type", "Popen")) func = subprocess.Popen resp = [] args = msg.get("args") print("Executing:", *args) try: proc = func(args, stdout=subprocess.PIPE) for line in proc.stdout.readlines(): if not line: break pr = line.decode().strip() print(pr) resp.append(pr) except Exception as e: resp.extend(("Error:", str(e))) return dumps(resp)
def next(self): block = bytearray() finish_time = time.time() + self.TIMEOUT while time.time() <= finish_time: left = self.BUFSIZE - len(block) data = self.fd.read(left) if data: block.extend(data) if len(block) == self.BUFSIZE: values = common.loads(str(block)) if self.check: self.check(values) return values time.sleep(self.WAIT) raise IOError('timeout')
def recv(): out = wave.record('-', stdout=wave.sp.PIPE).stdout while True: data = out.read(len(sig_dump)) if len(data) < len(sig_dump): return try: x = common.loads(data) except common.SaturationError as e: print('saturation: {}'.format(e)) continue x = x - np.mean(x) c = np.abs(np.dot(x, sig)) / (np.sqrt(0.5 * len(x)) * sigproc.norm(x)) z = np.dot(x, sig.conj()) / (0.5 * len(x)) amp = np.abs(z) phase = np.angle(z) peak = np.max(np.abs(x)) print('coherence={:.3f} amp={:.3f} phase={:.1f} peak={:.3f}'.format( c, amp, phase * 180 / np.pi, peak))
def _parse_resp(self, d: bytes): data = loads(d).get("args") return "\n".join(data)