Python 如何在多达3400台机器上加快脚本速度?
我有3400台机器需要向graphite发送数据,但我的脚本大约需要5分钟才能完成,我如何才能将其减少到3或2分钟,谢谢,下面的代码Python 如何在多达3400台机器上加快脚本速度?,python,multiprocessing,fabric,Python,Multiprocessing,Fabric,我有3400台机器需要向graphite发送数据,但我的脚本大约需要5分钟才能完成,我如何才能将其减少到3或2分钟,谢谢,下面的代码 def sendmsg(hostname,metric): a = mntdir + hostname + '/' + metric + '_median' with settings(hide('running'), warn_only=True): valueMedian = local(("cat %s
def sendmsg(hostname,metric):
a = mntdir + hostname + '/' + metric + '_median'
with settings(hide('running'), warn_only=True):
valueMedian = local(("cat %s|grep %s|awk '{print $2}'") % (a,now),capture=True)
local(("echo %s.%s_Median_90days %s %s >/dev/udp/20.26.2.18/2001 ") % (hostname,metric,valueMedian,unixdate))
if __name__=='__main__':
while True:
localtime = time.asctime( time.localtime(time.time()) )
date = datetime.datetime.now()
now1 = date.strftime("%H:%M")
print now1
now2 = date.strftime("%M")
with settings(hide('running'), warn_only=True):
unixdate = local("date +%s",capture=True)
if int(now2) % 5 == 0:
now = now1 + ':00'
p=Pool(100)
print now
for hostname in host:
for metric in metrics:
p.apply_async(sendmsg, args=(hostname,metric))
starttime = time.asctime( time.localtime(time.time()) )
print('Waiting for all job done...%s' % starttime)
p.close()
p.join()
stoptime = time.asctime( time.localtime(time.time()) )
print('sending completed...%s ' % stoptime)
time.sleep(60)
与您的产品大致相同
cat file | grep foo | awk '{print $2}'
并避免创建两个进程,这可能会有点帮助。
grep foo file |……
(猫的无用用法)哈哈,它减少到3+分钟,任何其他建议lol,2分钟就可以了better@KlausD.,这正是Ulrich建议的-放下这个form@Flasking,如果您使用的是plumbum,您可以尝试直接使用它的grep命令。您的本地函数做什么?
cat file | grep foo | awk '{print $2}'