python 批量创建线程_python 远程批量多线程paramiko 和 threading案例

初步理解多线程的好处

这两个例子告诉我们同样的事情,一个用了8s一个用了5s这就是多线程并发执行的好处。

paramiko 和 threading 多线程远程执行的基本案例

--[root@scsv01181 pythontest]# cat paramiko-threading.py

#!/usr/bin/python

#coding:utf-8

#from settings.py import *

import paramiko

import threading

import time

def tunction(ip,username,password,command):

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(ip,username= username,password=password)

stdin,stdout,stdeer = client.exec_command(command)

print stdout.read()

client.close()

def main(host_list,command):

thread_list = []

for ip,username,password in host_list:

t = threading.Thread(target = tunction,args = (ip,username,password,command))

thread_list.append(t)

for th in thread_list:

th.start()

for th in thread_list:

th.join()

if name == "main":

host_list = [

("10.133.107.8","gluster_zabbix","gluster"),

("10.133.107.9","gluster_zabbix","gluster"),

]

command = "ls /tmp/"

main(host_list,command)

==============================

优化:当我们的机器很多而且需要经常修改我们可以单独做一个配置文件

创建一个settings.py的配置文件

目录结构

--[root@scsv01181 pythontest]# cat settings.py

#!/usr/bin/python

#coding:utf-8

host_list = [

("10.13.17.8","glustqeqr_zabbix","gluqwester"),

("10.13.17.9","glustewrer_zabbix","glursdaster"),

]

command = "ls /var/"

代码

--[root@scsv01181 pythontest]# cat paramiko-threading.py

#!/usr/bin/python

#coding:utf-8

from settings import *

import paramiko

import threading

import time

def tunction(ip,username,password,command):

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(ip,username= username,password=password)

stdin,stdout,stdeer = client.exec_command(command)

print stdout.read()

client.close()

def main(host_list,command):

thread_list = []

for ip,username,password in host_list:

t = threading.Thread(target = tunction,args = (ip,username,password,command))

thread_list.append(t)

for th in thread_list:

th.start()

for th in thread_list:

th.join()

if name == "main":

main(host_list,command)

================================

还可以设置守护线程

--[root@scsv01181 pythontest]# cat paramiko-threading.py

#!/usr/bin/python

#coding:utf-8

from settings import *

import paramiko

import threading

import time

def tunction(ip,username,password,command):

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(ip,username= username,password=password)

stdin,stdout,stdeer = client.exec_command(command)

print stdout.read()

client.close()

def main(host_list,command):

thread_list = []

for ip,username,password in host_list:

t = threading.Thread(target = tunction,args = (ip,username,password,command))

thread_list.append(t)

for th in thread_list:

th.start()

for th in thread_list:

th.join()

if name == "main":

start_time = time.time()

m = threading.Thread(target = main,args = (host_list,command))

m.setDaemon(True)

m.start()

m.join()

end_time = time.time()

chg_time = str(start_time - end_time) + "s"

print chg_time

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值