python之multiprocessing:multiprocessing.Pool
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之multiprocessing:multiprocessing.Pool相关的知识,希望对你有一定的参考价值。
[实现:多进程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.实际运行
[[email protected] 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的主要内容,如果未能解决你的问题,请参考以下文章
内存使用量随着 Python 的 multiprocessing.pool 不断增长