Exemplo n.º 1
0
def get_distance(pubsub: PubSub) -> float:
    pubsub.subscribe("subsystem.sonar.measurement", ignore_subscribe_messages=True)
    timeout = 0.5
    start = time.time()
    while cycle([True]):
        if time.time() - start > timeout:
            logger.error("Unable to get sonar measurement!!")
            raise NoSonarException("Unable to get sonar measurement")
        redis_message = pubsub.get_message()
        if redis_message is not None:
            pubsub.unsubscribe("subsystem.sonar.measurement")
            message = json.loads(redis_message['data'])
            return float(message)
        sleep(0.01)
Exemplo n.º 2
0
class SubWebSocket(tornado.websocket.WebSocketHandler):
    def open(self, *args, **kwargs):
        self.pubsub = PubSub(REDIS_CONNECTION_POOL,
                             ignore_subscribe_messages=True)
        self.client = self.request.connection.context.address
        print("opened")

    def assemble_cmd(self, log_path, cmd):
        kill_cmd = "kill `ps aux|grep logtail.py|grep %s|grep -v grep|awk '{print $2}'`" % (
            log_path, )
        return "{kill};{tail}".format(kill=kill_cmd, tail=cmd)

    @gen.coroutine
    def on_message(self, message):
        local = get_local_client(io_loop=tornado.ioloop.IOLoop.instance())
        hostname, log_path, cmd = message.split("||")
        cmd = self.assemble_cmd(log_path, cmd)
        self.channel = settings.LOG_KEY.format(server=hostname.strip(),
                                               log_path=log_path.strip())
        self.pubsub.subscribe(**{self.channel: self.channel_callback})
        local.cmd_async(hostname, "cmd.run", [cmd])
        while self.pubsub.subscribed:
            if not self.pubsub.connection.can_read(timeout=0):
                yield gen.sleep(0.05)
            else:
                self.pubsub.get_message()
        self.pubsub.close()
        print 'pubsub closed'

    def channel_callback(self, message):
        line = format_line(message["data"])
        try:
            self.write_message(line)
        except tornado.websocket.WebSocketClosedError:
            self.pubsub.unsubscribe(self.channel)

    def on_close(self):
        self.pubsub.unsubscribe(self.channel)
        print("closed")
Exemplo n.º 3
0
def main():
    pubsub = PubSub(pool)
    pubsub.subscribe(['foo'])
    threading.Thread(target=changeFoo).start()
    for t in pubsub.listen():
        print t