zoukankan      html  css  js  c++  java
  • Python之路-python(paramiko,进程和线程的区别,GIL全局解释器锁,线程)

    一、paramiko

    二、进程、与线程区别

    三、python GIL全局解释器锁

    四、线程

      1. 语法
      2. join
      3. 线程锁之LockRlock信号量
      4. 将线程变为守护进程
      5. Event事件 
      6. queue队列
      7. 生产者消费者模型

      一、paramiko

      用于远程连接并执行简单的命令

      使用用户名密码连接:

     1 import paramiko
     2 
     3 # 创建SSH对象
     4 ssh = paramiko.SSHClient()
     5 # 允许连接不在know_hosts文件中的主机
     6 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     7 # 连接服务器
     8 ssh.connect(hostname='172.16.5.163', port=22, username='root', password='111111')
     9 
    10 # 执行命令
    11 stdin, stdout, stderr = ssh.exec_command('df')
    12 # 获取命令结果
    13 result = stdout.read()
    14 print(result.decode())
    15 
    16 # 关闭连接
    17 ssh.close()
    18 
    19 
    20 结果
    21 Filesystem                    1K-blocks    Used  Available Use% Mounted on
    22 /dev/mapper/VolGroup-lv_root   51606140 1518048   47466652   4% /
    23 tmpfs                            510172       0     510172   0% /dev/shm
    24 /dev/sda1                        495844   33461     436783   8% /boot
    25 /dev/mapper/VolGroup-lv_home 2059640248  203016 1954813516   1% /home

      使用公钥连接

    import paramiko
    
    private_key = paramiko.RSAKey.from_private_key_file('id_rsa.txt')
    
    #创建SSH对象
    ssh = paramiko.SSHClient()
    #允许连接不在know_host文件中的主机
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    #连接服务器
    ssh.connect(hostname='172.16.5.163',port=22,username='root',pkey=private_key)
    
    #执行命令
    stdin,stdout,stderr = ssh.exec_command('df -h')
    
    #获取命令结果
    restult = stdout.read()
    
    #打印执行结果
    print(restult.decode())
    
    #关闭连接
    ssh.close()

      SFTPClient使用用户名密码完成上传下载

     1 import paramiko
     2 
     3 transport = paramiko.Transport(('172.16.5.163',22))
     4 transport.connect(username='root',password='111111')
     5 
     6 sftp = paramiko.SFTPClient.from_transport(transport)
     7 # 将location.py 上传至服务器 /tmp/test.py
     8 sftp.put('D:\test1\put.txt', '/tmp/put.txt')
     9 # 将remove_path 下载到本地 local_path
    10 sftp.get('/tmp/get.txt', 'D:\test1\get.txt')
    11 
    12 transport.close()

      SFTPClient使用公钥完成上传下载

     1 import paramiko
     2 
     3 private_key = paramiko.RSAKey.from_private_key_file('id_rsa.txt')
     4 
     5 transport = paramiko.Transport(('172.16.5.163', 22))
     6 transport.connect(username='root', pkey=private_key )
     7 
     8 sftp = paramiko.SFTPClient.from_transport(transport)
     9 # 将location.py 上传至服务器 /tmp/test.py
    10 sftp.put('D:\test1\put.txt', '/tmp/put.txt')
    11 # 将remove_path 下载到本地 local_path
    12 sftp.get('/tmp/get.txt', 'D:\test1\get.txt')
    13 
    14 transport.close()

      

      二、进程、与线程区别

      线程:是操作系统的最小调度单元,一堆指令的集合。

      进程:操作CPU,必须先创建一个线程

    进程和线程的区别
    
    启动一个线程比启动一个进程快,运行速度没有可比性。
    先有一个进程然后才能有线程。
    1、进程包含线程
    2、线程共享内存空间
    3、进程内存是独立的(不可互相访问)
    4、进程可以生成子进程,子进程之间互相不能互相访问(相当于在父级进程克隆两个子进程)
    5、在一个进程里面线程之间可以交流。两个进程想通信,必须通过一个中间代理来实现
    6、创建新线程很简单,创建新进程需要对其父进程进行克隆。
    7、一个线程可以控制或操作同一个进程里面的其它线程。但进程只能操作子进程。
    8、父进程可以修改不影响子进程,但不能修改。

       三、python GIL全局解释器锁

    In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

    上面的核心意思就是,无论你启多少个线程,你有多少个cpu, Python在执行的时候会淡定的在同一时刻只允许一个线程运行,擦。。。,那这还叫什么多线程呀?莫如此早的下结结论,听我现场讲。

    首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把GIL归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL

    详细说明:http://www.dabeaz.com/python/UnderstandingGIL.pdf 

      其实就是为了解决同一时间内多个线程处理一个运算(浪费资源),全局解释器锁解决同一时间如果有线程执行过了,其它线程就不重复运算。

      四、线程

      

    1. 语法

    写法一:

     1 import  threading
     2 
     3 #创建一个函数,每个线程要使用的函数
     4 def fun(n):
     5     print(n)
     6 
     7 n1 = threading.Thread(target=fun,args=('n1',))#生成一个线程
     8 n2 = threading.Thread(target=fun,args=('n2',))#再生成一个线程
     9 
    10 n1.start()#启动n1这个线程
    11 n2.start()#启动n2这个线程
    12 
    13 print(n1.getName())#获取线程名
    14 print(n2.getName())#获取线程名

    写法二:

    import  threading,time
    
    class mythread(threading.Thread):#继承thread.Thread这个函数
        def __init__(self,n):
            super(mythread,self).__init__()
            self.n = n
            
        def run(self):#这里这个方法名必须命名为run,否则失败
            print('运行线程',self.n)
    
    n1 = mythread("n1")
    n2 = mythread("n2")
    
    n1.start()
    n2.start()

       2、join

      用法例如线程n1和n2,想要等n1的结果在执行n2,就需要n1.join。具体如下

    
    
     1 import  threading,time
     2 
     3 class mythread(threading.Thread):#继承thread.Thread这个函数
     4     def __init__(self,n,sleep_time):
     5         super(mythread,self).__init__()
     6         self.n = n
     7         self.sleep_time = sleep_time
     8 
     9 
    10     def run(self):#这里这个方法名必须命名为run,否则失败
    11         print("开始运行线程",self.n)
    12         time.sleep(self.sleep_time)#执行完一个线程后停止2秒
    13         print('运行线程结束',self.n,threading.current_thread())
    14 
    15 
    16 n1 = mythread("n1",2)
    17 n2 = mythread("n2",4)
    18 
    19 n1.start()
    20 n2.start()
    21 n1.join
    22 print('这里是主程序',threading.current_thread())
    
    
    1 开始运行线程 n1
    2 开始运行线程 n2
    3 这里是主程序 <_MainThread(MainThread, started 12624)>#一个进程启动收首先会打印主线程
    4 运行线程结束 n1 <mythread(Thread-1, started 4020)>#这里是普通线程
    5 运行线程结束 n2 <mythread(Thread-2, started 9088)>#这里是普通线程
     

      3、线程锁之LockRlock信号量

      线程锁:保证同一时间内只有一个线程可以修改一个数据(避免同一时间内多个线程修改同一个数据)

      4、将线程变为守护进程

     1 import threading
     2 import time
     3 
     4 def run(n):
     5     print("task",n)
     6     time.sleep(2)
     7     print('')
     8 
     9 
    10 
    11 start_time = time.time()
    12 t_obj = []
    13 for i in range(50):
    14     t = threading.Thread(target=run,args=("t--%s"%i,))
    15     t.setDaemon(True)#把当前线程线程设置为守护进程,必须要写在start前面,否则报错
    16     t.start()
    17     t_obj.append(t)
    18 
    19 
    20 print("主线程执行")#主线程
    21 print("cost:",time.time() - start_time)
    22 
    23 #非守护进程退出了,就全部退出(这里是主线程执行完毕后,就不等子线程了,主线程退出了,就都退出了)

      线程锁(互斥锁Mutex)

      一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况?

     1 import time
     2 import threading
     3  
     4 def addNum():
     5     global num #在每个线程中都获取这个全局变量
     6     print('--get num:',num )
     7     time.sleep(1)
     8     num  -=1 #对此公共变量进行-1操作
     9  
    10 num = 100  #设定一个共享变量
    11 thread_list = []
    12 for i in range(100):
    13     t = threading.Thread(target=addNum)
    14     t.start()
    15     thread_list.append(t)
    16  
    17 for t in thread_list: #等待所有线程执行完毕
    18     t.join()
    19  
    20  
    21 print('final num:', num )

    正常来讲,这个num结果应该是0, 但在python 2.7上多运行几次,会发现,最后打印出来的num结果不总是0,为什么每次运行的结果不一样呢? 哈,很简单,假设你有A,B两个线程,此时都 要对num 进行减1操作, 由于2个线程是并发同时运行的,所以2个线程很有可能同时拿走了num=100这个初始变量交给cpu去运算,当A线程去处完的结果是99,但此时B线程运算完的结果也是99,两个线程同时CPU运算的结果再赋值给num变量后,结果就都是99。那怎么办呢? 很简单,每个线程在要修改公共数据时,为了避免自己在还没改完的时候别人也来修改此数据,可以给这个数据加一把锁, 这样其它线程想修改此数据时就必须等待你修改完毕并把锁释放掉后才能再访问此数据。 

    *注:不要在3.x上运行,不知为什么,3.x上的结果总是正确的,可能是自动加了锁

      加锁版本

    import time
    import threading
     
    def addNum():
        global num #在每个线程中都获取这个全局变量
        print('--get num:',num )
        time.sleep(1)
        lock.acquire() #修改数据前加锁
        num  -=1 #对此公共变量进行-1操作
        lock.release() #修改后释放
     
    num = 100  #设定一个共享变量
    thread_list = []
    lock = threading.Lock() #生成全局锁
    for i in range(100):
        t = threading.Thread(target=addNum)
        t.start()
        thread_list.append(t)
     
    for t in thread_list: #等待所有线程执行完毕
        t.join()
     
    print('final num:', num )
     

       

      GIL VS Lock 

      机智的同学可能会问到这个问题,就是既然你之前说过了,Python已经有一个GIL来保证同一时间只能有一个线程来执行了,为什么这里还需要lock? 注意啦,这里的lock是用户级的lock,跟那个GIL没关系 ,具体我们通过下图来看一下+配合我现场讲给大家,就明白了。

      RLock(递归锁)

      一把大锁中还有很多小锁

      

     1 import threading,time
     2  
     3 def run1():
     4     print("grab the first part data")
     5     lock.acquire()
     6     global num
     7     num +=1
     8     lock.release()
     9     return num
    10 def run2():
    11     print("grab the second part data")
    12     lock.acquire()
    13     global  num2
    14     num2+=1
    15     lock.release()
    16     return num2
    17 def run3():
    18     lock.acquire()
    19     res = run1()
    20     print('--------between run1 and run2-----')
    21     res2 = run2()
    22     lock.release()
    23     print(res,res2)
    24  
    25  
    26 if __name__ == '__main__':
    27  
    28     num,num2 = 0,0
    29     lock = threading.RLock()
    30     for i in range(10):
    31         t = threading.Thread(target=run3)
    32         t.start()
    33  
    34 while threading.active_count() != 1:
    35     print(threading.active_count())
    36 else:
    37     print('----all threads done---')
    38     print(num,num2)

      信号量:同一时间内,最大允许多个线程运行

     1 import threading,time
     2 
     3 def run(n):
     4     semaphore.acquire()
     5     time.sleep(1)
     6     print("run the thread: %s
    " %n)
     7     semaphore.release()
     8 
     9 
    10 semaphore  = threading.BoundedSemaphore(5) #最多允许5个线程同时运行
    11 for i in range(19):
    12     t = threading.Thread(target=run,args=(i,))
    13     t.start()
    14 
    15 while threading.active_count() != 1:
    16     pass #print threading.active_count()
    17 else:
    18     print('----all threads done---')

      5、Event事件 

      通过Event来实现两个或多个线程间的交互,下面是一个红绿灯的例子,即起动一个线程做交通指挥灯,生成几个线程做车辆,车辆行驶按红灯停,绿灯行的规则。

      

    #event.wait()等待
    #event.clear() #把标志位清空
    #event.is_set()设定标志位
     1 import threading,time
     2 event = threading.Event()
     3 
     4 def lighter():
     5     count = 0
     6     event.set() #先设定绿灯
     7     while True:
     8         if count >5 and count < 10:#改为红灯
     9             event.clear() #把标志位清空
    10             print("33[41;1m     33[0m")
    11         elif count > 10:
    12             event.set() #变绿灯
    13             count = 0
    14         else:
    15             print("33[42;1m     33[0m")
    16         time.sleep(1)
    17         count += 1
    18 def car(name):
    19     while True:
    20         if event.is_set():#代表绿灯
    21             print("[%s] running"%name)
    22             time.sleep(1)
    23         else:
    24             print("[%s] 红灯稍等"%name)
    25             event.wait()
    26             print("033[34;1m[%s] green light is on,start going...33[0m"%name)
    27 lighter = threading.Thread(target=lighter,)
    28 lighter.start()
    29 car1 = threading.Thread(target=car,args=("Tesla",))
    30 car1.start()
    View Code

      6、queue队列

      提高运行效率,完成程序的解耦。

     1 >>> import queue
     2 >>> q = queue.Queue()#生成实例
     3 >>> q.put('d1')#存第一个硬盘
     4 >>> q.put('d2')#存第二个硬盘
     5 >>> q.put('d3')#存第三个硬盘
     6 >>> q.qsize()#获取队列大小
     7 3
     8 >>> q.get()#取值(先存先取,没有就卡住),可以通过异常,来获取
     9 'd1'
    10 >>>
    11 >>> q.get()
    12 'd2'
    13 >>> q.get()

    queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.

    class queue.Queue(maxsize=0) #先入先出
    class queue.LifoQueue(maxsize=0) #last in fisrt out 
    class queue.PriorityQueue(maxsize=0) #存储数据时可设置优先级的队列

    Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

    The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).

    exception queue.Empty

    Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.

    exception queue.Full

    Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

    Queue.qsize()
    Queue.empty() #return True if empty  
    Queue.full() # return True if full 
    Queue.put(itemblock=Truetimeout=None)

    Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

    Queue.put_nowait(item)

    Equivalent to put(item, False).

    Queue.get(block=Truetimeout=None)

    Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

    Queue.get_nowait()

    Equivalent to get(False).

    Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

    Queue.task_done()

    Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

    If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

    Raises a ValueError if called more times than there were items placed in the queue.

    Queue.join() block直到queue被消费完毕

      7、生产者消费者模型

    在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题。该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度。

    为什么要使用生产者和消费者模式

    在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程。在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据。同样的道理,如果消费者的处理能力大于生产者,那么消费者就必须等待生产者。为了解决这个问题于是引入了生产者和消费者模式。

    什么是生产者消费者模式

    生产者消费者模式是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。

     1 import threading,time
     2 
     3 import queue
     4 
     5 q = queue.Queue(maxsize=10)
     6 
     7 def Producer(name):
     8     count = 1
     9     while True:
    10         q.put("骨头%s" % count)
    11         print("生产了骨头",count)
    12         count +=1
    13         time.sleep(0.1)
    14 
    15 
    16 
    17 def  Consumer(name):
    18     #while q.qsize()>0:
    19     while True:
    20         print("[%s] 取到[%s] 并且吃了它..." %(name, q.get()))
    21         time.sleep(1)
    22 
    23 
    24 
    25 p = threading.Thread(target=Producer,args=("Alex",))
    26 c = threading.Thread(target=Consumer,args=("ChengRonghua",))
    27 c1 = threading.Thread(target=Consumer,args=("王森",))
    28 
    29 
    30 p.start()
    31 c.start()
    32 c1.start()
  • 相关阅读:
    混合开发的坑(3) ---关于es6
    混合开发的坑(2) ---pdf展示
    混合开发的坑(1) ---ajax请求
    vue.js
    vue中 import引入组件
    vue中 静态文件引用注意事项
    Oracle 数据库链接
    Oracle中的NVL,NVL2,NULLIF以及COALESCE函数使用
    Merge into 使用
    C# —— IList, ArrayList与List的区别详解
  • 原文地址:https://www.cnblogs.com/lei0213/p/5885328.html
Copyright © 2011-2022 走看看