python笔记一一进程,线程

21/4/7

     1.进程,线程

          paramiko       模拟Linux在python上进行文件的上传与下载

import paramiko
#创建SSH对象
ssh = paramiko.SSHClient()
#允许连接不在know_hosts文件的主机
#ssh.set_missing_host_key_polocy(paramiko.AutoAddPolicy())
#连接服务器
ssh.connect(hostname='localhost', port=22, username='root',password='123456')
#执行命令
stdin,stdout,stderr = ssh.exec_command('df')
#获取命令结果
result = stdout.read()
#关闭连接
ssh.close()
import paramiko
transport = paramiko.Transport(('hostname',22))
transport.connect(username='root',password='123456')

stfp = paramiko.SFTPClient.form_transport(transport)
#将location.py 上传至服务器  /tmp/test.py
stfp.put('/tmp/location.py', '/tmp/test.py')
#将remove_path  下载到本地  local_path
stfp.get('remove_path','local_path')

transport.close()

基于公钥的连接:

import paramiko

private_key = paramiko.RSAKey.from_private_key_file('/home/wangteng/.shh/id_rsa')

#创建SSH对象
ssh = paramiko.SSHCLIENT()
#允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接服务器
ssh.connect(hostname='localhost',port=22,username='wangteng',key=private_key)

#执行命令
stdin,stdout,stder = ssh.exec_command('df')
#获取命令结果
result = stdout.read()

#关闭连接
ssh.close()

多线程

import threading
import time
def run(n):
    print("task",n)
    #time.sleep(2)

t1 = threading.Thread(target=run,args=("t1",))
t2 = threading.Thread(target=run,args=("t2",))

t1.start()
t2.start()

使用类的形式进行创建

import threading

class MyThread(threading.Thread):
    def __init__(self,n):
        super(MyThread,self).__init__()
        self.n = n
    def run (self):
        print("runnint task",self.n)

t1 = MyThread("t1")
t2 = MyThread("t2")

t1.start()
t2.start()

实例1:进行多线程的时间统计分析

import threading
import time
def run(n):
    print("task",n)
    time.sleep(2)
start_time = time.time()
t_objs = []  #创建一个新列表,  存线程实例
for i in range(50):
    t = threading.Thread(target=run,args=("t-%s" %i,))
    t.start()
    t_objs.append(t)

for t in t_objs:
    t.join()

print("----------all threads has finished...")
print("cost:",time.time() - start_time)

实例2:线程锁(在py2.*版本上试,3.*版本看不出来)

import  time
import threading
def run(n):
    lock.acquire()  #上锁
    global num
    num +=1
    time.sleep(1)
    lock.release()   #解锁

lock = threading.Lock()
num = 0
t_objs = []  #创建一个新列表,  存线程实例
for i in range(5):
    t = threading.Thread(target=run,args=("t-%s" %i,))
    t.start()
    t_objs.append(t)  #为了不阻塞后面进程的启动,不在这里join,先放到一个列表里

for t in t_objs:   #循环线程实例列表,等待所有的线程执行完毕
    t.join()

print("----------all threads has finished...",threading.current_thread())

print("num:",num)

递归锁RLock(就是一个大锁中还包含子锁)

import threading,time

def run1():
    print("grab the first part data")
    lock.acquire()
    global num
    num +=1
    lock.release()
    return num
def run2():
    print("grab the second part data")
    lock.acquire()
    global num2
    num2 +=1
    lock.release()
    return num2
def run3():              #run3主锁  run1,run2子锁
    lock.acquire()
    res = run1()
    print("-----------between run1 and run2--------------")
    res2 = run2()
    lock.release()
    print(res,res2)

if __name__ == '__main__':
    num,num2 = 0,0
    lock = threading.RLock()
    for i in range(10):
        t = threading.Thread(target=run3)
        t.start()

while threading.active_count()  != 1:
    print(threading.active_count())
else:
    print('----------all threads done-----------')
    print(num,num2)

守护进程Daemon    主线程退出时,守护进程也会跟着一起退出

import time
import threading
 
 
def run(n):
 
    print('[%s]------running----\n' % n)
    time.sleep(2)
    print('--done--')
 
def main():
    for i in range(5):
        t = threading.Thread(target=run,args=[i,])
        t.start()
        t.join(1)
        print('starting thread', t.getName())
 
 
m = threading.Thread(target=main,args=[])
m.setDaemon(True) #将main线程设置为Daemon线程,它做为程序主线程的守护线程,当主线程退出时,m线程也会退出,由m启动的其它子线程会同时退出,不管是否执行完任务
m.start()
m.join(timeout=2)
print("---main thread done----")

信号量semaphore    (可以同时允许一定数量的线程修改数据)

import threading,time

def run(n):
    semaphore.acquire()
    time.sleep(1)
    print("run the thread: %s\n" % n)
    semaphore.release()

if __name__ == '__main__':

    semaphore = threading.BoundedSemaphore(5) #最多允许5个线程同时进行
    for i in range(20):
        t = threading.Thread(target=run, args=(i,))
        t.start()

while threading.active_count() !=1:
    pass
else:
    print("----------all threads done ---------")

Events事件:进行不同线程之间同步

实例:模拟红绿灯

import threading,time

event = threading.Event()

def lighter():
    count = 0
    event.set()  #先设置为绿灯
    while True:
        if count > 5 and count < 10:  #改为红灯
            event.clear()  #将标志位清除
            print("\033[41;1mred light is on ...\033[0m")
        elif count > 10:
            event.set()  #变绿灯
            count = 0
        else:
            print("\033[42;1mgreen light is on ...\033[0m")
        time.sleep(1)
        count +=1

def car(name):
    while True:
        if event.is_set():   #代表绿灯
            print("[%s] running...." %name )
            time.sleep(1)
        else:
            print("[%s] sees red light ,waitting...." %name)
            event.wait()
            print("\033[34;1m[%s] green light is on ,start going...\033[0m" %name)

light = threading.Thread(target=lighter,)
light.start()

car1 = threading.Thread(target=car,args=("兰博基尼",))
car1.start()

队列queue         作用:  解耦:使程序直接实现松耦合。  提高处理效率     (FIFO  LIFO)     

                          列表和队列的区别:列表取出数据,实际上数据还在列表里面,但队列的话,取出了就是真的取出了,队列中就没有了

实例:狗吃骨头

import threading,time
import queue

q = queue.Queue(maxsize=10)    #定义队列对多容许多大
def Producer(name):     #一直生产骨头
    count = 1
    while True:
        q.put("骨头%s" % count)
        print(" 生产了骨头", count)
        count += 1
        time.sleep(0.5)   #生产速度

def Consumer(name):
    while True:
        print ("[%s] 取到[%s] 并且吃了它..." %(name,q.get()))
        time.sleep(1)

p = threading.Thread(target=Producer,args=("WT",))
a = threading.Thread(target=Consumer,args=("Dog1",))
a1 = threading.Thread(target=Consumer,args=("Dog2",))

p.start()
a.start()
a1.start()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不淘气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值