cli.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:xcute 作者: viert 项目源码 文件源码
def run_parallel(self, hosts, cmd):
        codes = {"total": 0, "error": 0, "success": 0}

        def worker(host, cmd):
            p = Popen(self.get_parallel_ssh_options(host, cmd), stdout=PIPE, stderr=PIPE)
            while True:
                outs, _, _ = select([p.stdout, p.stderr], [], [])
                if p.stdout in outs:
                    outline = p.stdout.readline()
                else:
                    outline = ""
                if p.stderr in outs:
                    errline = p.stderr.readline()
                else:
                    errline = ""

                if outline == "" and errline == "" and p.poll() is not None:
                    break

                if outline != "":
                    print("%s: %s" % (colored(host, "blue", attrs=["bold"]), outline.strip()))
                if errline != "":
                    print("%s: %s" % (colored(host, "blue", attrs=["bold"]), colored(errline.strip(), "red")))
            if p.poll() == 0:
                codes["success"] += 1
            else:
                codes["error"] += 1
            codes["total"] += 1

        pool = Pool(self.ssh_threads)
        for host in hosts:
            pool.start(Greenlet(worker, host, cmd))
        pool.join()
        self.print_exec_results(codes)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号