首页 > 编程语言 > 详细

Ansible源码分析之Python的multiprocessing模块使用

时间:2016-03-14 01:58:43      阅读:413      评论:0      收藏:0      [点我收藏+]


最近在研究Python的multiprocessing模块,顺便去了解下Ansible是如何实现并发执行命令的。

在Ansible目录下搜索multiprocessing

cd /usr/lib/python2.6/site-packages/ansible

find -exec grep -l multiprocessing {} \;


只有./runner/__init__.py这个文件里面引用了multiprocessing模块


def _parallel_exec(self, hosts):
        ‘‘‘ handles mulitprocessing when more than 1 fork is required ‘‘‘

        manager = multiprocessing.Manager()
        job_queue = manager.Queue()
        for host in hosts:
            job_queue.put(host)
        result_queue = manager.Queue()

        try:
            fileno = sys.stdin.fileno()
        except ValueError:
            fileno = None

        workers = []
        for i in range(self.forks):
            new_stdin = None
            if fileno is not None:
                try:
                    new_stdin = os.fdopen(os.dup(fileno))
                except OSError, e:
                    # couldn‘t dupe stdin, most likely because it‘s
                    # not a valid file descriptor, so we just rely on
                    # using the one that was passed in
                    pass
            prc = multiprocessing.Process(target=_executor_hook,
                args=(job_queue, result_queue, new_stdin))
            prc.start()
            workers.append(prc)

        try:
            for worker in workers:
                worker.join()
        except KeyboardInterrupt:
            for worker in workers:
                worker.terminate()
                worker.join()

        results = []
        try:
            while not result_queue.empty():
                results.append(result_queue.get(block=False))
        except socket.error:
            raise errors.AnsibleError("<interrupted>")
        return results

    # *****************************************************





Ansible能够同时对成百上千的服务器同时执行操作,都是通过这个函数实现的,使用Python的多进程模块来提升效率

首先定义一个











参考资料:


本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1750652

Ansible源码分析之Python的multiprocessing模块使用

原文:http://john88wang.blog.51cto.com/2165294/1750652

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!