Python 运维开发 学习记录 paramiko和多线程、Queue(六)

Shell/Python   paramiko   多线程  

一、paramiko 模块

  • 安装
    git clone https://github.com/paramiko/paramiko
    cd paramiko
    easy_install ./

  • 密码登陆

#!/usr/bin/python
#_*_coding:utf-8_*_

import paramiko  
import sys,os

host = sys.argv[1]  
user = 'leo'  
password = '123456'  
port = 2022  
cmd = sys.argv[2]

s = paramiko.SSHClient()                                    #绑定实例  
#s.load_system_host_keys()                                   #加载本机的Knowhost主机文件
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     #自动对新主机生成数字签名

s.connect(host,port,user,password,timeout=5)  
stdin,stdout,stderr = s.exec_command(cmd)  
cmd_result = stdout.read(),stderr.read()

for line in cmd_result:  
    print "\n\033[1;32m %s \033[0m" % line,

s.close()  
  • key登陆
#!/usr/bin/python
#_*_coding:utf-8_*_

import paramiko  
import sys,os

host = sys.argv[1]  
user = 'leo'  
password = '123456'  
port = 2002  
cmd = sys.argv[2]

s = paramiko.SSHClient()                                    #绑定实例  
s.load_system_host_keys()                                   #加载本机的Knowhost主机文件  
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())     #自动对新主机生成数字签名  
pkey_file= '/root/.ssh/id_rsa'  
key = paramiko.RSAKey.from_private_key_file(pkey_file)  
s.connect(host,port,user,pkey=key,timeout=5)  
stdin,stdout,stderr = s.exec_command(cmd)  
cmd_result = stdout.read(),stderr.read()

for line in cmd_result:  
    print "\n\033[1;32m %s \033[0m" % line,

s.close()  

二、多线程

  • threading 因为GIL(全局解释器锁:保证同一时间只能有一个线程运行)
  • 线程threading.lock
#!/usr/bin/python
#_*_ coding:utf-8 _*_

import threading  
import time

def run(num):  
    global NUM
    time.sleep(1)
    lock.acquire()
    print "Hi,I am thread %s ..lalala" % num
    NUM += 1
    lock.release()

NUM = 0  
p_list = []  
lock = threading.Lock()

for i in range(200):  
    t = threading.Thread(target=run,args=(i,))
    t.start()
    p_list.append(t)

for i in p_list:    #并行  
    i.join()

print "---->",NUM  
  • threading.rlock 递归锁
#!/usr/bin/python
#_*_ coding:utf-8 _*_

import threading  
import time

def run1():  
    global num1
    print "Hi,I am 1 thread %s ..lalala" % num1
    time.sleep(1)
    lock.acquire()


    num1 += 1
    lock.release()

    return  num1

def run2():  
    global num2
    print "Hi,I am 2  thread %s ..lalala" % num2
    time.sleep(1)
    lock.acquire()

    num2 += 1
    lock.release()
    return num2


def run3():

    time.sleep(1)
    lock.acquire()
    res = run1()
    print "run1 and run2"
    res2 = run2()
    lock.release()
    print res,res2

num1,num2 = 0,0  
lock = threading.RLock()

for i in range(200):  
    t = threading.Thread(target=run3)
    t.start()
    #join 串行

   # join  并行

print "---->"  
  • threading.event
import threading,time,random

def light():  
    if not event.isSet():
        event.set()         #wait 就不阻塞,绿灯状态
    count = 0
    while True:
        if count < 10:
            print "\033[42;1m--green light on ----\033[0m"
        elif count < 13:
            print "\033[43;1m--yellow light on ----\033[0m"
        elif count < 20:
            if event.isSet():
                event.clear()     #清除绿灯,打开红灯
            print "\033[41;1m--red light on----\033[0m"
        else:
            count = 0
            event.set()
        time.sleep(1)
        count += 1

def car(n):  
    while 1:
        time.sleep(random.randrange(5))
        if event.isSet():
            print "car [%s] is running..." % n
        else:
            print "car [%s] is waiting for the red light...." % n
            event.wait()
            print "Green list is on ,car  [%s] is running........" % n

if __name__ == "__main__":  
    event = threading.Event()
    Ligit = threading.Thread(target=light)
    Ligit.start()
    for i in range(3):
        t = threading.Thread(target=car,args=(i,))
        t.start()
  • Queue 队列
#!/usr/bin/python
#_*_ coding:utf-8 _*_
import Queue,random  
import time,threading  
q = Queue.Queue()

def Producer(name):  
    count = 0
    while True:
        time.sleep(random.randrange(3))
        if q.qsize() < 3:       #队列大小
            q.put(count)        #添加到队列
            print "Producer %s has produced %s baozi" % (name,count)
            count+=1

def Consumer(name):

    while True:
        time.sleep(random.randrange(4))
        if not q.empty():   # 队列不为空
            data = q.get()  # 从队列中取
            #print data
            print "\033[32;1mConsumer %s has eat %s baozi..\033[0m" %(name,data)
        else:
            print "\033[31;1m %s no baozi eat\033[0m" % name

p1 = threading.Thread(target=Producer,args=('minggong',))  
c1 = threading.Thread(target=Consumer,args=('leo',))  
c2 = threading.Thread(target=Consumer,args=('hong',))  
p1.start()  
c1.start()  
c2.start()