花很长时间使用python执行saltstack命令 [英] Taking a long time to execute a saltstack command using python
问题描述
我正在尝试执行此saltstack命令
cmd = "/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/"
output = os.popen(cmd).readlines()
使用python.
我已经并行启动了8个线程来运行此命令,但是每个线程花费大约5分钟来完成.
但是我是在终端机或python控制台中独立运行此命令的,大约需要2-3秒才能完成.
首先,我认为这是因为readlines()占用大量内存,但是当我删除readlines()只是为了使用os.popen()运行shell命令时,它仍然要花费5分钟的时间才能完成.
我不知道为什么要花这么多时间来运行此命令,对此有什么解决方案?谢谢你们
I was trying to execute this saltstack command
cmd = "/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/"
output = os.popen(cmd).readlines()
using python.
I have started 8 threads in parallel to run this command, but each thread costs about 5 min to finish.
But I run this command in terminal or python console standalone, it just takes me about 2-3 secs to complete.
First I thought it was because of readlines() consuming lots of memory, but it still costs 5 min more to finish when I removed readlines() just to run the shell command with os.popen().
I have no idea why it takes so much time to run this command, any solutions to this? Thx guys
我发现执行output = os.popen("/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/")
而不是output = os.popen(cmd)
花费了大约5秒钟的时间.
变量cmd
与"/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/"
完全相同.
为什么会有如此大的差异?
I have found that it consumes around 5 sec to execute output = os.popen("/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/")
other than output = os.popen(cmd)
.
The variable cmd
is exactly the same with "/usr/local/bin/salt -L 'target' cp.get_dir salt://allapps/V001 /u02/tomcat-8.0.24/8080/webapps/"
.
Why there is so much difference?
推荐答案
我不确定为什么该命令要花这么长时间.您是否考虑过使用Salt的python API? https://docs.saltstack.com/en/latest /ref/clients/index.html#python-api
I'm not sure why that command is taking so long. Have you considered using Salt's python api? https://docs.saltstack.com/en/latest/ref/clients/index.html#python-api
这篇关于花很长时间使用python执行saltstack命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!