def _recv(): while True: try: ln = _recv_q.get() try: ln = ln.strip() if ln: r, _ = gs.json_decode(ln, {}) token = r.get('token', '') k = REQUEST_PREFIX+token req = gs.attr(k) gs.del_attr(k) if req and req.f: gs.debug(DOMAIN, "margo response: method: %s, token: %s, dur: %0.3fs, err: `%s'" % ( req.method, req.token, (time.time() - req.tm), r.get('error', ''), )) keep = req.f(r.get('data', {}), r.get('error', '')) is not True if keep: req.tm = time.time() gs.set_attr(k, req) else: gs.debug(DOMAIN, 'Ignoring margo: token: %s' % token) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def _recv(): while True: try: ln = _recv_q.get() try: ln = ln.strip() if ln: r, _ = gs.json_decode(ln, {}) token = r.get('token', '') k = REQUEST_PREFIX + token req = gs.attr(k) gs.del_attr(k) if req and req.f: gs.debug( DOMAIN, "margo response: method: %s, token: %s, dur: %0.3fs, err: `%s'" % ( req.method, req.token, (time.time() - req.tm), r.get('error', ''), )) keep = req.f(r.get('data', {}), r.get('error', '')) is not True if keep: req.tm = time.time() gs.set_attr(k, req) else: gs.debug(DOMAIN, 'Ignoring margo: token: %s' % token) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def _send(): while True: try: try: method, arg, cb = _send_q.get() proc = gs.attr(PROC_ATTR_NAME) if not proc or proc.poll() is not None: if proc: try: proc.kill() proc.stdout.close() except: pass maybe_install() if not gs.checked(DOMAIN, 'launch _recv'): gsq.launch(DOMAIN, _recv) proc, _, err = gsshell.proc([MARGO9_BIN, '-poll=30'], stderr=gs.LOGFILE, env={ 'XDG_CONFIG_HOME': gs.home_path(), }) gs.set_attr(PROC_ATTR_NAME, proc) if not proc: gs.notice(DOMAIN, 'Cannot MarGo: %s' % err) continue gsq.launch(DOMAIN, lambda: _read_stdout(proc)) req = Request(f=cb, method=method) gs.set_attr(REQUEST_PREFIX + req.token, req) gs.debug( DOMAIN, 'margo request: method: %s, token: %s' % (req.method, req.token)) header, _ = gs.json_encode({ 'method': method, 'token': req.token }) body, _ = gs.json_encode(arg) ln = '%s %s\n' % (header, body) proc.stdin.write(ln) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def _send(): while True: try: try: method, arg, cb = _send_q.get() proc = gs.attr(PROC_ATTR_NAME) if not proc or proc.poll() is not None: if proc: try: proc.kill() proc.stdout.close() except: pass maybe_install() if not gs.checked(DOMAIN, 'launch _recv'): gsq.launch(DOMAIN, _recv) proc, _, err = gsshell.proc([MARGO9_BIN, '-poll=30'], stderr=gs.LOGFILE ,env={ 'XDG_CONFIG_HOME': gs.home_path(), }) gs.set_attr(PROC_ATTR_NAME, proc) if not proc: gs.notice(DOMAIN, 'Cannot MarGo: %s' % err) continue gsq.launch(DOMAIN, lambda: _read_stdout(proc)) req = Request(f=cb, method=method) gs.set_attr(REQUEST_PREFIX+req.token, req) gs.debug(DOMAIN, 'margo request: method: %s, token: %s' % (req.method, req.token)) header, _ = gs.json_encode({'method': method, 'token': req.token}) body, _ = gs.json_encode(arg) ln = '%s %s\n' % (header, body) proc.stdin.write(ln) except Exception: gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break
def _send(): while True: try: try: method, arg, cb = _send_q.get() proc = gs.attr(PROC_ATTR_NAME) if not proc or proc.poll() is not None: killSrv() maybe_install() if not gs.checked(DOMAIN, "launch _recv"): gsq.launch(DOMAIN, _recv) proc, _, err = gsshell.proc( [MARGO9_BIN, "-poll=30"], stderr=gs.LOGFILE, env={"XDG_CONFIG_HOME": gs.home_path()} ) gs.set_attr(PROC_ATTR_NAME, proc) if not proc: gs.notice(DOMAIN, "Cannot start MarGo: %s" % err) try: cb({}, "Abort. Cannot start MarGo") except: pass continue gsq.launch(DOMAIN, lambda: _read_stdout(proc)) req = Request(f=cb, method=method) gs.set_attr(REQUEST_PREFIX + req.token, req) gs.debug(DOMAIN, "margo request: method: %s, token: %s" % (req.method, req.token)) header, _ = gs.json_encode({"method": method, "token": req.token}) body, _ = gs.json_encode(arg) ln = "%s %s\n" % (header, body) proc.stdin.write(ln) except Exception: killSrv() gs.println(gs.traceback()) except Exception: gs.println(gs.traceback()) break