Python bash中xargs的并行处理
我有一个小脚本,可以在其中向每个openstack的租户提供源代码,并在python的帮助下获取一些输出。生成报告的时间太长,建议我使用Python bash中xargs的并行处理,python,bash,openstack,xargs,Python,Bash,Openstack,Xargs,我有一个小脚本,可以在其中向每个openstack的租户提供源代码,并在python的帮助下获取一些输出。生成报告的时间太长,建议我使用xargs。我以前的代码如下所示 #!/bin/bash cd /scripts/cloud01/floating_list rm -rf ./reports/openstack_reports/ mkdir -p ./reports/openstack_reports/ source ../creds/base for tenant in A B C D
xargs
。我以前的代码如下所示
#!/bin/bash
cd /scripts/cloud01/floating_list
rm -rf ./reports/openstack_reports/
mkdir -p ./reports/openstack_reports/
source ../creds/base
for tenant in A B C D E F G H I J K L M N O P Q R S T
do
source ../creds/$tenant
python ../tools/openstack_resource_list.py > ./reports/openstack_reports/$tenant.html
done
lftp -f ./lftp_script
现在我在脚本中加入了xargs,脚本看起来像这样
#!/bin/bash
cd /scripts/cloud01/floating_list
rm -rf ./reports/openstack_reports/
mkdir -p ./reports/openstack_reports/
source ../creds/base
# Need xargs idea below
cat tenants_list.txt | xargs -P 8 -I '{}' # something that takes the tenant name and source
TENANT_NAME={}
python ../tools/openstack_resource_list.py > ./reports/openstack_reports/$tenant.html
lftp -f ./lftp_script
在这个脚本中,我应该如何实现
source../creds/$tenant
?因为在处理每个租户的同时,还需要对其进行资源化,我不确定如何将其与xargs一起用于并行执行 xargs
无法轻松运行shell函数。。。但它可以运行一个shell
# If the tenant names are this simple, don't put them in a file
printf '%s\n' {A..T} |
xargs -P 8 -I {} bash -c 'source ../creds/"$0"
python ../tools/openstack_resource_list.py > ./reports/openstack_reports/"$0".html' {}
在脚本中,bash-c'…'
之后的参数被公开为$0
如果您想将租户保存在一个文件中,
xargs-a文件名
是一个很好的方法来避免这种情况,尽管它不能移植到所有xargs
实现中。(使用GNU Parallel使用xargs…重定向,看起来如下所示:
#!/bin/bash
cd /scripts/cloud01/floating_list
rm -rf ./reports/openstack_reports/
mkdir -p ./reports/openstack_reports/
source ../creds/base
doit() {
source ../creds/"$1"
python ../tools/openstack_resource_list.py > ./reports/openstack_reports/"$1".html
}
env_parallel doit ::: {A..T}
lftp -f ./lftp_script
env_parallel
将环境复制到每个命令(包括函数)中。然后,它运行parallel
,每个内核并行运行一个作业
根据任务的不同,并行运行更多或更少可能更快或更慢。使用-j8
对8个并行作业进行调整,或使用-j200%
对每个核心2个作业进行调整。非常感谢。我得到了预期的结果。接受您的答案。这确实会让我陷入一些其他问题,这些问题来自我的python文件,但这是另一个问题r故事:添加xargs增加了我请求数据的服务器上的CPU负载。我增加了10秒的睡眠时间来制造一个小的延迟。你对此有什么建议吗?根据定义,并行处理会增加负载。如果远程服务器没有准备好处理这种负载,那么可以降低并行性,可能只有一点(4而不是8?),但如果对整个系统更方便的话,最终会返回序列化。如果您的Python脚本访问远程服务器,并且Python API允许您设置优先级或“良好”级别,这将允许您与服务器通信,如果负载较高,这将允许花费更长的时间;但这已经进入了一个投机国家,我并没有权力将优先权设定在好的水平上。我还需要更快地生成报告,但要将负载保持在最低限度。这就像一个僵局(
#!/bin/bash
cd /scripts/cloud01/floating_list
rm -rf ./reports/openstack_reports/
mkdir -p ./reports/openstack_reports/
source ../creds/base
doit() {
source ../creds/"$1"
python ../tools/openstack_resource_list.py > ./reports/openstack_reports/"$1".html
}
env_parallel doit ::: {A..T}
lftp -f ./lftp_script