zoukankan      html  css  js  c++  java
  • Day27:threading模块

    一、threading模块

    1、线程对象的创建

    1.1 Thread类直接创建

    import threading
    import time
    
    def countNum(n): # 定义某个线程要运行的函数
    
        print("running on number:%s" %n)
    
        time.sleep(3)
    
    if __name__ == '__main__':
    
        t1 = threading.Thread(target=countNum,args=(23,)) #生成一个线程实例
        t2 = threading.Thread(target=countNum,args=(34,))
    
        t1.start() #启动线程
        t2.start()
    
        print("ending!")

    1.2 Thread类继承式创建

    #继承Thread式创建
    
    import threading
    import time
    
    class MyThread(threading.Thread):
    
        def __init__(self,num):
            threading.Thread.__init__(self)
            self.num=num
    
        def run(self):
            print("running on number:%s" %self.num)
            time.sleep(3)
    
    t1=MyThread(56)
    t2=MyThread(78)
    
    t1.start()
    t2.start()
    print("ending")

    2、Thread类的实例方法

    2.1 join() 和 steDaemon()

    # join():在子线程完成运行之前,这个子线程的父线程将一直被阻塞。
    
    # setDaemon(True):
            '''
             将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。
    
             当我们在程序运行中,执行一个主线程,如果主线程又创建一个子线程,主线程和子线程 就分兵两路,分别运行,那么当主线程完成
    
             想退出时,会检验子线程是否完成。如果子线程未完成,则主线程会等待子线程完成后再退出。但是有时候我们需要的是只要主线程
    
             完成了,不管子线程是否完成,都要和主线程一起退出,这时就可以 用setDaemon方法啦'''
    
    import threading
    from time import ctime,sleep
    import time
    
    def Music(name):
    
            print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
            sleep(3)
            print("end listening {time}".format(time=ctime()))
    
    def Blog(title):
    
            print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
            sleep(5)
            print('end recording {time}'.format(time=ctime()))
    
    
    threads = []
    
    
    t1 = threading.Thread(target=Music,args=('FILL ME',))
    t2 = threading.Thread(target=Blog,args=('',))
    
    threads.append(t1)
    threads.append(t2)
    if __name__ == '__main__':
    
        for t in threads:
            t.start()
    
        print ("all over %s" %ctime())
    '''
    运行结果
    Begin listening to FILL ME. Tue Jul 18 16:15:06 2017
    Begin recording the . Tue Jul 18 16:15:06 2017
    all over Tue Jul 18 16:15:06 2017
    end listening Tue Jul 18 16:15:09 2017
    end recording Tue Jul 18 16:15:11 2017
    
    
    
    前三行瞬间执行完毕,后两行等待3秒和5秒执行
    
    '''
    if __name__ == '__main__':
    
        for t in threads:
            t.setDaemon(True) #注意:一定在start之前设置
            t.start()
    
        print("all over %s" % ctime())
    '''
    运行结果:
    Begin listening to FILL ME. Tue Jul 18 16:31:23 2017
    Begin recording the . Tue Jul 18 16:31:23 2017
    all over Tue Jul 18 16:31:23 201
    
    每个线程都是守护线程,跟随主线程一块挂掉
    '''
    if __name__ == '__main__':
    
        # t2.setDaemon(True)
    
        for t in threads:
            # t.setDaemon(True) #注意:一定在start之前设置
            t.start()
    
        t1.join()
    
        print("all over %s" % ctime())
    '''
    运行结果:
    Begin listening to FILL ME. Tue Jul 18 16:34:41 2017
    Begin recording the . Tue Jul 18 16:34:41 2017
    end listening Tue Jul 18 16:34:44 2017
    all over Tue Jul 18 16:34:44 2017
    end recording Tue Jul 18 16:34:46 2017
    
    前两行瞬间执行完成后等待,第二三行一起执行,最后一行最后出现。
    在t1.join()处阻塞,t1线程运行完毕,主线程才继续执行。
    '''
    daemon
    A boolean value indicating whether this thread is a daemon thread (True) or not (False). This must be set before start() is called, otherwise RuntimeError is raised. Its initial value is inherited from the creating thread; the main thread is not a daemon thread and therefore all threads created in the main thread default to daemon = False.
    
    The entire Python program exits when no alive non-daemon threads are left.
    
    当daemon被设置为True时,如果主线程退出,那么子线程也将跟着退出,
    
    反之,子线程将继续运行,直到正常退出。
    daemon

    2.2 其他方法

    Thread实例对象的方法
      # isAlive(): 返回线程是否活动的。
      # getName(): 返回线程名。
      # setName(): 设置线程名。
    
    threading模块提供的一些方法:
      # threading.currentThread(): 返回当前的线程变量。
      # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
      # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

    3、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.)
    
    翻译:
    在CPython中,全局解释器锁是一个互斥锁,它可以防止多个本机线程同时执行Python的编译器。这个锁是必需的,主要是因为CPython的内存管理不是线程安全的。(然而,由于GIL的存在,其他的特性已经发展到依赖于它的保证。)
    '''
    View Code

    Python中的线程是操作系统的原生线程,Python虚拟机使用一个全局解释器锁(Global Interpreter Lock)来互斥线程对Python虚拟机的使用。为了支持多线程机制,一个基本的要求就是需要实现不同线程对共享资源访问的互斥,所以引入了GIL。
    GIL:在一个线程拥有了解释器的访问权之后,其他的所有线程都必须等待它释放解释器的访问权,即使这些线程的下一条指令并不会互相影响。
    在调用任何Python C API之前,要先获得GIL
    GIL缺点:多处理器退化为单处理器;优点:避免大量的加锁解锁操作。

    3.1 GIL的早期设计

    Python支持多线程,而解决多线程之间数据完整性和状态同步的最简单方法自然就是加锁。 于是有了GIL这把超级大锁,而当越来越多的代码库开发者接受了这种设定后,他们开始大量依赖这种特性(即默认python内部对象是thread-safe的,无需在实现时考虑额外的内存锁和同步操作)。慢慢的这种实现方式被发现是蛋疼且低效的。但当大家试图去拆分和去除GIL的时候,发现大量库代码开发者已经重度依赖GIL而非常难以去除了。有多难?做个类比,像MySQL这样的“小项目”为了把Buffer Pool Mutex这把大锁拆分成各个小锁也花了从5.5到5.6再到5.7多个大版为期近5年的时间,并且仍在继续。MySQL这个背后有公司支持且有固定开发团队的产品走的如此艰难,那又更何况Python这样核心开发和代码贡献者高度社区化的团队呢?

    3.2 GIL的影响

    无论你启多少个线程,你有多少个cpu, Python在执行一个进程的时候会淡定的在同一时刻只允许一个线程运行。
    所以,python是无法利用多核CPU实现多线程的。
    这样,python对于计算密集型的任务开多线程的效率甚至不如串行(没有大量切换),但是,对于IO密集型的任务效率还是有显著提升的。

    #coding:utf8
    from threading import Thread
    import time
    
    def counter():
        i = 0
        for _ in range(50000000):
            i = i + 1
    
        return True
    
    
    def main():
    
        l=[]
        start_time = time.time()
    
        for i in range(2):
    
            t = Thread(target=counter)
            t.start()
            l.append(t)
            t.join()
    
        # for t in l:
        #     t.join()
    
        end_time = time.time()
        print("Total time: {}".format(end_time - start_time))
    
    if __name__ == '__main__':
        main()
    
    
    '''
    py2.7:
         串行:25.4523348808s
         并发:31.4084379673s
    py3.5:
         串行:8.62115597724914s
         并发:8.99609899520874s
    
    '''
    计算密集型

    3.3 解决方案

    用multiprocessing替代Thread multiprocessing库的出现很大程度上是为了弥补thread库因为GIL而低效的缺陷。它完整的复制了一套thread所提供的接口方便迁移。唯一的不同就是它使用了多进程而不是多线程。每个进程有自己的独立的GIL,因此也不会出现进程之间的GIL争抢。

    当然multiprocessing也不是万能良药。它的引入会增加程序实现时线程间数据通讯和同步的困难。就拿计数器来举例子,如果我们要多个线程累加同一个变量,对于thread来说,申明一个global变量,用thread.Lock的context包裹住三行就搞定了。而multiprocessing由于进程之间无法看到对方的数据,只能通过在主线程申明一个Queue,put再get或者用share memory的方法。这个额外的实现成本使得本来就非常痛苦的多线程程序编码,变得更加痛苦了。
    
    总结:因为GIL的存在,只有IO Bound场景下得多线程会得到较好的性能 - 如果对并行计算性能较高的程序可以考虑把核心部分换成C模块,或者索性用其他语言实现 - GIL在较长一段时间内将会继续存在,但是会不断对其进行改进。
    
    所以对于GIL,既然不能反抗,那就学会去享受它吧!
    
    4、同步锁(Lock)
    使用multiprocessing
    import time
    import threading
    def addNum():
        global num #在每个线程中都获取这个全局变量
        #num-=1
    
        temp=num
        time.sleep(0.1)
        num =temp-1  # 对此公共变量进行-1操作
    
    num = 100  #设定一个共享变量
    
    thread_list = []
    
    s=time.time()
    for i in range(100):
        t = threading.Thread(target=addNum)
        t.start()
        thread_list.append(t)
    
    for t in thread_list: #等待所有线程执行完毕
        t.join()
    
    print('Result: ', num ,'cost time: ' ,time.time()-s)
    #运行结果
    #Result:  99 cost time:  0.11100625991821289

    分析结果:

    绿色框代表进程,蓝色框代表子线程,一共开了100个子线程(不包括主线程)。

    开启一个子线程并运行后,temp被赋值为100,然后遇到阻塞,其他子线程抢到CPU进行执行,此时num没有执行-1操作,所以线程2 的temp也被赋值为100,然后遇到阻塞。其他线程抢CPU执行。0.1秒的时间足够100个线程都将temp赋值为100,然后再执行-1操作。所以num = 100 - 1 ,到结束num=99。

    解决方法:使用同步锁对数据进行保护

    锁通常被用来实现对共享资源的同步访问。为每一个共享资源创建一个Lock对象,当你需要访问该资源时,调用acquire方法来获取锁对象(如果其它线程已经获得了该锁,则当前线程需等待其被释放),待资源访问完后,再调用release方法释放锁:

    import threading
    
    R=threading.Lock()
    
    R.acquire()
    '''
    对公共数据的操作
    '''
    R.release()
    import time
    import threading
    def addNum():
        global num #在每个线程中都获取这个全局变量
        #num-=1
        
        R.acquire()    #保护数据,串行执行
        temp=num
        time.sleep(0.1)
        num =temp-1  # 对此公共变量进行-1操作
        R.release()
    
    num = 100  #设定一个共享变量
    thread_list = []
    
    R=threading.Lock()    #实例化锁对象
    
    s=time.time()
    for i in range(100):
        t = threading.Thread(target=addNum)
        t.start()
        thread_list.append(t)
    
    for t in thread_list: #等待所有线程执行完毕
        t.join()
    
    print('Result: ', num ,'cost time: ' ,time.time()-s)
    #运行结果
    #Result:  0 cost time:  10.023573398590088    
    '''
    1、为什么有了GIL,还需要线程同步?
    
    多线程环境下必须存在资源的竞争,那么如何才能保证同一时刻只有一个线程对共享资源进行存取?
    
    加锁, 对, 加锁可以保证存取操作的唯一性, 从而保证同一时刻只有一个线程对共享数据存取.
    
    通常加锁也有2种不同的粒度的锁:
    
        coarse-grained(粗粒度): python解释器层面维护着一个全局的锁机制,用来保证线程安全。
                                内核级通过GIL实现的互斥保护了内核的共享资源。
    
        fine-grained(细粒度):   那么程序员需要自行地加,解锁来保证线程安全,
                                用户级通过自行加锁保护的用户程序的共享资源。
    
     2、GIL为什么限定在一个进程上?
     
     你写一个py程序,运行起来本身就是一个进程,这个进程是由解释器来翻译的,所以GIL限定在当前进程;
     如果又创建了一个子进程,那么两个进程是完全独立的,这个字进程也是有python解释器来运行的,所以
     这个子进程上也是受GIL影响的                
    
    
    '''
    扩展思考

    5、死锁与递归锁

    所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。

    import threading,time
    
    LockA=threading.Lock()      #定义锁A
    LockB=threading.Lock()      #定义锁B
    
    class MyThread(threading.Thread):
    
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run(self):
            self.foo()
            self.bar()
    
        def foo(self):
            LockA.acquire()     #加A锁
            print('I am foo %s get LOCKA-------%s'%(self.name,time.ctime()))
    
            LockB.acquire()     #加B锁
            print('I am foo %s get LOCKB-------%s' % (self.name, time.ctime()))
            LockB.release()     #解B锁
    
            LockA.release()     #解A锁,A锁被线程2拿到
    
        def bar(self):
            LockB.acquire()     #加B锁
            print('I am bar %s get LOCKB-------%s' % (self.name, time.ctime()))
    
            LockA.acquire()     #需要加A锁,但A锁被线程2占用,线程2需要加B锁,相互拿不到锁,造成死锁
            print('I am bar %s get LOCKA-------%s' % (self.name, time.ctime()))
            LockA.release()
    
            LockB.release()for i in range(10):
        t=MyThread()
        t.start()
    '''
    运行结果:
    I am foo Thread-1 get LOCKA-------Tue Jul 18 18:22:26 2017
    I am foo Thread-1 get LOCKB-------Tue Jul 18 18:22:26 2017
    I am bar Thread-1 get LOCKB-------Tue Jul 18 18:22:26 2017
    I am foo Thread-2 get LOCKA-------Tue Jul 18 18:22:26 2017
    '''

     解决方案:使用递归锁

    import threading,time
    
    Rlock=threading.RLock()  #定义递归锁
    
    class MyThread(threading.Thread):
    
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run(self):
            self.foo()
            self.bar()
    
        def foo(self):
            Rlock.acquire()     #递归锁counter+1
            print('I am foo %s get LOCKA-------%s'%(self.name,time.ctime()))
    
            Rlock.acquire()     #递归锁counter+1
            print('I am foo %s get LOCKB-------%s' % (self.name, time.ctime()))
            Rlock.release()     #递归锁counter-1
    
            Rlock.release()     #递归锁counter-1  递归锁counter为零,可被其他线程获取
    
        def bar(self):
            Rlock.acquire()     #递归锁counter+1
            print('I am bar %s get LOCKB-------%s' % (self.name, time.ctime()))
    
            Rlock.acquire()     #递归锁counter+1
            print('I am bar %s get LOCKA-------%s' % (self.name, time.ctime()))
            Rlock.release()   #递归锁counter-1
    
            Rlock.release()   #递归锁counter-1
    
    for i in range(10):
        t=MyThread()
        t.start()
    '''
    运行结果:
    I am foo Thread-1 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-1 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am foo Thread-2 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-2 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-1 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-1 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-3 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-3 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-2 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-2 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-4 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-4 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-4 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-4 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am bar Thread-3 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-3 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-6 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-6 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-6 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-6 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-5 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-5 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-5 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-5 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-9 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-9 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-9 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-9 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-7 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-7 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-7 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-7 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-10 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-10 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-10 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-10 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-8 get LOCKA-------Tue Jul 18 18:30:01 2017
    I am foo Thread-8 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-8 get LOCKB-------Tue Jul 18 18:30:01 2017
    I am bar Thread-8 get LOCKA-------Tue Jul 18 18:30:01 2017
    '''
    
    运行结果
    运行结果

    在Python中为了支持在同一线程中多次请求同一资源,python提供了可重入锁RLock。这个RLock内部维护着一个Lock和一个counter变量,counter记录了acquire的次数,从而使得资源可以被多次require。直到一个线程所有的acquire都被release,其他的线程才能获得资源。

    6、Semaphore(信号量)

    Semaphore管理一个内置的计数器,
    每当调用acquire()时内置计数器-1;
    调用release() 时内置计数器+1;
    计数器不能小于0;当计数器为0时,acquire()将阻塞线程直到其他线程调用release()。

    实例:(同时只有5个线程可以获得semaphore,即可以限制最大连接数为5):

    import threading
    import time
    
    semaphore = threading.Semaphore(5)
    
    def func():
        if semaphore.acquire():
            print (threading.currentThread().getName() + ' get semaphore')
            time.sleep(2)
            semaphore.release()
    
    for i in range(20):
      t1 = threading.Thread(target=func)
      t1.start()
    '''
    运行结果:
    Thread-1 get semaphore
    Thread-2 get semaphore
    Thread-3 get semaphore
    Thread-4 get semaphore
    Thread-5 get semaphore
    Thread-6 get semaphore
    Thread-8 get semaphore
    Thread-7 get semaphore
    Thread-10 get semaphore
    Thread-9 get semaphore
    Thread-12 get semaphore
    Thread-11 get semaphore
    Thread-13 get semaphore
    Thread-14 get semaphore
    Thread-15 get semaphore
    Thread-16 get semaphore
    Thread-17 get semaphore
    Thread-18 get semaphore
    Thread-19 get semaphore
    Thread-20 get semaphore
    
    每5个一起打印
    '''
  • 相关阅读:
    继承与多态,Instanceof关键字
    面向对象,单例模式
    方法
    数组
    流程控制
    基础语法
    连接linux四大远程工具
    MYSQL-索引的设计
    银行一类(Ⅰ类)、二类(Ⅱ类)、三类(Ⅲ类)账户区别是什么?
    真正有效的学习
  • 原文地址:https://www.cnblogs.com/Vee-Wang/p/7206824.html
Copyright © 2011-2022 走看看