Pool多进程示例

时间:2021-04-21 20:44:44

  利用Pool类多进程实现批量主机管理

 #!/usr/bin/python
# -*- coding: UTF-8 -*-
# Author: standby
# Time: 2017-03-02
# Description: Achieve the Multiple Processes(High Concurrent) to execute single or multiple commands functions by pool class of Python Lang. import time
import commands, subprocess
import os, re, sys
import paramiko
from multiprocessing import Pool # print color
COLOR_PINK = '\033[95m'
COLOR_BLUE = '\033[94m'
COLOR_GREEN = '\033[92m'
COLOR_YELLOW = '\033[93m'
COLOR_RED = '\033[91m'
COLOR_DEFAULT = '\033[0m' def Check_IP(ip):
ip_str = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
if re.match(ip_str, ip):
return True
else:
return False def Get_IPS(ipfile):
'''Generate ip list.'''
ips = []
with open(ipfile) as iplist:
content = iplist.readlines()
for line in content:
if line.startswith('#'):
continue
elif Check_IP(line.strip('\n')):
ips.append(line.strip('\n'))
else:
print '%s is invalid ip address!' % line.strip('\n')
continue
return ips def concurrentFunc(ip, cmd):
'''single cmd to exec...'''
#RET = subprocess.check_output("ssh root@%s 2> /dev/null cmd" % ip, shell = True)
#status, output = commands.getstatusoutput("ssh root@%s 2> /dev/null cmd" % ip)
#status, output = commands.getstatusoutput("ssh root@%s 2> /dev/null cmd
" % ip)
#return ip+": "+output+", status: "+bytes(status)
#return ip+": "+RET
'''multiple cmd to exec...'''
PORT = 22
USER = "root"
KEY = "/path/known_hosts"
ssh = paramiko.SSHClient()
try:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, PORT, USER, KEY, timeout=10)
except paramiko.AuthenticationException:
print ip+" ssh timeout, continue..."
return "SSH ERROR, exit..."
output = []
output.append(ip)
for m in cmd:
stdin, stdout, stderr = ssh.exec_command(m)
output.append(stdout.readlines())
return output def callBackFunc(ret):
print "This is callback func of %s" % ret[0] def output(res_list):
print "%s=================RESULT====================%s" % (COLOR_GREEN, COLOR_DEFAULT)
for res in res_list:
try:
print res.get()[0] + " -> " + ''.join(res.get()[1])
except Exception, e:
print "%sOUTPUT ERROR: %s %s" % (COLOR_YELLOW, e, COLOR_DEFAULT)
continue if __name__ == '__main__':
ipfile = sys.argv[1]
if os.path.isfile(ipfile):
ips = Get_IPS(ipfile)
elif Check_IP(ipfile):
ips = [sys.argv[1]]
else:
print '%s is invalid ip address!' % ipfile
sys.exit(1) res_list = []
#cmd = ['cmd1', 'cmd2']
cmd = ['cmd']
t_start=time.time()
#pool = Pool(32)
pool = Pool(10)
for ip in ips:
#维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去
res = pool.apply_async(func=concurrentFunc, args=(ip, cmd,), callback=callBackFunc)
res_list.append(res)
pool.close()
pool.join()
pool.terminate()
output(res_list)
t_end=time.time()
t=t_end-t_start
print '%sDealt %d, used time is :%s.%s' % (COLOR_BLUE, len(res_list), t, COLOR_DEFAULT)