【问题标题】:How can I get this to run in parallel using Fabric?我怎样才能让它使用 Fabric 并行运行?
【发布时间】:2016-10-19 03:01:33
【问题描述】:

我之前问过一个相关的问题:

How to issue commands on remote hosts in parallel using Fabric without using a fabfile?

我在多个测试主机上并行执行测试时遇到问题。

我的代码如下所示:

@parallel  
def run_test(arg_list):
    # arg_list is a list of dictionary.  Each entry in
    # arg_list has a 'ip_address' and a 'test_config_file'

    for x in arg_list:
        ip_address = x['ip_address']
        test_config_file = x['test_config_file']
        env['host_string'] = ip_address
        # The test program "test_localhost.py" is already on all the Test hosts
        cmd = "/root/test_localhost.py --ip_addr=" + ip_address + " --config=" + test_config_file
        run(cmd)


if __name__ == '__main__':

    env.parallel = True

    # Each test host will have unique test_config_files
    arg_list = list()
    arg_list.append({'ip_address':'10.10.10.10', 'test_config_file': "config_01.json"})
    arg_list.append({'ip_address':'10.10.10.11', 'test_config_file': "config_02.json"})

    execute(run_test, arg_list)

我已针对 2 个以上的测试主机运行此代码。我有一个脚本可以检查测试是否在测试主机上运行。测试不是并行运行的。

相反,测试是按顺序运行的——“test_localhost.py”首先在 10.10.10.10 上运行,然后在完成后在 10.10.10.11 上运行。

我还需要做些什么来使测试并行运行吗?

注意:我不能使用 fabfile,因为我为每个测试主机发送不同的测试配置文件。

【问题讨论】:

    标签: python parallel-processing fabric


    【解决方案1】:

    这就是我要做的。不同主机的不同参数的技巧是将信息添加到env context_manager 以让您获取任务的主机特定参数。只需确保主机与您用于字典的键匹配,并且并行命令可以正常工作。最后,fabric 中的任务通常通过fab 命令运行。我从未使用原生 python 脚本尝试过它,并且不确定后果或是否需要进行任何特殊处理。 Fabric 项目的典型格式是在名为 fabfile.py 的文件中定义任务,并且可以使用 fab 运行这些任务。

    在一个名为fabfile.py的文件中:

    from fabric.decorators import task, parallel
    from fabric.operations import run
    from fabric.context_managers import env
    
    @task
    def run_test(arg_list):
        # arg_list is a list of dictionary.  Each entry in
        # arg_list has a 'ip_address' and a 'test_config_file'
        env.hosts = [ x['ip_address'] for x in arg_list ]
        env.host_data = dict()
        for x in arg_list:
            env.host_data[x['ip_address']] = x
        execute(run_command)
    
    @parallel
    def run_command():
        context = env.host_data[env.host]
        cmd = '/root/test_localhost.py --ip_addr=%(ip_address)s --config=%(test_config_file)s' % context
        run(cmd)
    
    @task 
    def run_my_test():
        arg_list.append({'ip_address':'10.10.10.10', 'test_config_file': "config_01.json"})
        arg_list.append({'ip_address':'10.10.10.11', 'test_config_file': "config_02.json"})
        run_test(arg_list)
    

    从命令行运行:

    fab run_my_test
    

    【讨论】:

    • 2ps,感谢您的建议,但我不能使用 fabfile,因为需要传递配置文件,然后由脚本解析。就我而言, fabfile.py 需要采用运行时参数。
    • fabric 命令可以采用运行时参数。只需将参数添加到用@task 装饰的函数即可。
    • 2ps: env.host_data 定义了吗?我编写的脚本与您的脚本非常相似,但 Python 在这一行抱怨: env.host_data[x['ip_address']] = x 我使用的是 Python 2.7,我从 fabric.context_manager 导入了 env 模块
    • Ick,你说得对,我忘了这行:env.host_data = {}
    • ps2,感谢您的帮助。我可以在我的测试脚本中使用与您类似的代码。我可以使用 Fabric 在多个测试主机上并行启动任务,但不必使用 fabfile 或调用“fab run_my_test”。再次感谢。
    猜你喜欢
    • 2021-05-27
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2019-09-01
    • 2021-12-30
    • 2021-11-12
    相关资源
    最近更新 更多