如何在不使用fabfile的情况下使用Fabric在远程主机上并行发出命令? [英] How to issue commands on remote hosts in parallel using Fabric without using a fabfile?
问题描述
我有一个Python脚本,该脚本使用Fabric在远程主机上启动测试,获取测试的otuput文件,并进行一些解析. Python脚本不是fabfile.
I have a Python script that uses Fabric to launch tests on remote hosts, get the otuput file of the tests, and do some parsing. The Python script is not a fabfile.
我想并行启动和运行测试.我已经读过有关使用"@parallel"装饰器的信息,但是我读过的所有示例都将脚本作为fabfile.
I would like to launch and run the tests in parallel. I've read about using the "@parallel" decorator but all the examples I've read has the script as a fabfile.
我的代码是这样的:
from fabric.api import *
# Copy the testfile on each of the hosts. This is sequential, it could be
# done in parallel but doing it in parallel is not that important
def copy_test(host_list, testfile_name):
for x in host_list:
env['host_string'] = x
target_testfile_name = "/tmp/" + testfile_name
put(testfile_name, target_testfile_name)
@parallel # Would this decorator work?
def run_test(host_list, testfile_name):
target_testfile_name = "/tmp/" + testfile_name
for x in host_list:
env['host_string'] = x
run(target_testfile_name)
if __name__ == '__main__':
HOSTS = ['10.10.10.10', '10.10.10.11', '10.10.10.12', '10.10.10.13']
testfile_name = "foo.py"
copy_test(HOSTS, testfile_name)
# I would like to launch run_test() in parallel
run_test(HOSTS, testfile_name)
这是代码的简化版本.我并没有包含所有内容,但是我传递了主机的配置信息,因此限制了我将此脚本用作fabfile并在其中发出类似以下内容的信息:
This is a simplified version of the code. I have not included everything but I pass around configuration information of the hosts so that limits me in using this script as a fabfile where I issue something like:
"fab -H '10 '10 .10.10'copy_test"
"fab -H '10.10.10.10' copy_test"
"fab -H '10 '10 .10.10'run_test" -P
"fab -H '10.10.10.10' run_test" -P
我可以使用threading.Threads库执行run_test(),但我宁愿做为最后的选择.
I could execute run_test() using the threading.Threads library but I would rather do that as a last resort.
如您所见,我没有将它作为fabfile运行.
As you can see, I am not running this as a fabfile.
有没有一种方法可以使用Fabric的并行执行模型执行run_test()而无需将脚本作为"fabfile"执行?
Is there a way I could execute run_test() using Fabric's parallel execution model without executing my script as a "fabfile"?
推荐答案
我无法发表评论,所以我将答案更改为:
I can't comment so i'll put an answer change your main to :
if __name__ == '__main__':
HOSTS = ['10.10.10.10', '10.10.10.11', '10.10.10.12', '10.10.10.13']
testfile_name = "foo.py"
execute(copy_test,HOSTS, testfile_name)
# I would like to launch run_test() in parallel
execute(run_test,HOSTS, testfile_name)
如果您使用execute()
调用该功能,而您的功能具有@parallel
,则它将并行启动
if you call the fonction with execute()
and your fonction has the @parallel
it'll be launched in parallel
这篇关于如何在不使用fabfile的情况下使用Fabric在远程主机上并行发出命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!