1. 程式人生 > >python之multiprocessing(二):multiprocessing.Pool

python之multiprocessing(二):multiprocessing.Pool

centos6 bsp ins keys name roc set turn system

[實現:多進程paramiko]

1.server_list.txt :格式 HOST PORT USER PASSWD

localhost 22 root root
193.168.49.144 22 root root

2.get_server_list.py

server_list_file = "server_list.txt"
def get_serverlist_dic():
server_dic = {}
f = file(server_list_file)
for line in f.readlines():
if len(line.strip()) == 0:break
server_dic[line.split()[0]] = line.split()[1:]
f.close()
return server_dic
server_dic = get_serverlist_dic()
#print server_dic

3.single_paramiko.py

#!/usr/bin/env python
#encoding:utf8
import paramiko
import sys,os,time

host = sys.argv[1]
port = int(sys.argv[2])
user = sys.argv[3]
password = sys.argv[4]
cmd = str(sys.argv[5])

msg = "---------Result:%s----------" %host


p_client = paramiko.SSHClient() #綁定實例
p_client.load_system_host_keys() #加載本機HOST主機文件
p_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
p_client.connect(host,port,user,password,timeout=5)
stdin,stderr,stdout = p_client.exec_command(cmd)

cmd_result = stdout.read(),stderr.read()
print msg

for line in cmd_result:
print line,

except paramiko.AuthenticationException,e:
print msg
print "message:" + str(e)
sys.exit()
except paramiko.BadHostKeyException,e:
print msg
print "Bad host key" + str(e)

p_client.close()

4.multi_async.py

import multiprocessing
import os,sys,time
import get_server_list

cmd = sys.argv[1]
result = []

def runCmd(h,port,user,passwd,command):

cmd = "python single_paramiko.py %s %s %s %s %s" %(h,port,user,passwd,command)
print cmd
os.system(cmd)
p = multiprocessing.Pool(processes=250)

server_dic = get_server_list.server_dic

for host,values in server_dic.items():
ssh_port = values[0]
username = values[1]
password = values[2]
result.append(p.apply_async(runCmd,(host,ssh_port,username,password,cmd,)))

for res in result:
res.get(timeout=35)

5.實際運行

[root@centos6 multiprocess]# python multi_async.py ls
python single_paramiko.py 193.168.49.144 22 root root ls
python single_paramiko.py localhost 22 root root ls
---------Result:localhost----------
aaa.a
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
.....

.....
---------Result:193.168.49.144----------
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
.....

.....

python之multiprocessing(二):multiprocessing.Pool