zoukankan      html  css  js  c++  java
  • Day 30 GIL解释器锁/多线程的作用/死锁/递归锁/信号量/线程队列

    GIL全局解释器锁

    GIL全局解释器锁:
    基于Cpython来研究全局解释器锁.

    1.GIL本质上是一个互斥锁.
    2.GIL的为了阻止同一个进程内多个线程同时执行(并行)

    - 单个进程下的多个线程无法实现并行,但能实现并发
    

    3.这把锁主要是因为CPython的内存管理不是 "线程安全" 的.
    - 内存管理
    - 垃圾回收机制
    GIL的存在就是为了保证线程安全的.

    注意: 多个线程过来执行,一旦遇到IO操作,就会立马释放GIL解释器锁,交给下一个先进来的线程.

    验证多线程的作用

    计算密集型

    单核:

    ​ 开启进程:

    from multiprocessing import Process
    import os
    import time
    
    
    def work1():
        num = 0
        for i in range(10000000):
            num += 1
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(6):
            p = Process(target=work1)
            p.start()
            p_l.append(p)
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    ​ 开启线程:

    from threading import Thread
    import os
    import time
    
    
    def work1():
        num = 0
        for i in range(10000000):
            num += 1
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(6):
            p = Thread(target=work1)
            p.start()
            p_l.append(p)
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    多核:

    ​ 开启进程:

    from multiprocessing import Process
    import os
    import time
    
    
    def work1():
        num = 0
        for i in range(10000000):
            num += 1
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(6):
            p = Process(target=work1)
            p.start()
            p_l.append(p)
    
        for p in p_l:
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    ​ 开启线程:

    from threading import Thread
    import os
    import time
    
    
    def work1():
        num = 0
        for i in range(10000000):
            num += 1
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(6):
            p = Thread(target=work1)
            p.start()
            p_l.append(p)
          
        for p in p_l:
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    IO密集型

    单核:

    ​ 开启进程:

    from multiprocessing import Process
    import os
    import time
    
    
    def work2():
        time.sleep(1)
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(40):
            p = Process(target=work2)
            p.start()
            p_l.append(p)
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    ​ 开启线程:

    from threading import Thread
    import os
    import time
    
    
    def work2():
        time.sleep(1)
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(40):
            p = Thread(target=work2)
            p.start()
            p_l.append(p)
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    多核:

    ​ 开启进程:

    from multiprocessing import Process
    import os
    import time
    
    
    def work2():
        time.sleep(1)
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(40):
            p = Process(target=work2)
            p.start()
            p_l.append(p)
            
        for p in p_l:
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    ​ 开启线程:

    from threading import Thread
    import os
    import time
    
    
    def work2():
        time.sleep(1)
    
    
    if __name__ == '__main__':
        print(os.cpu_count())  # 6
        start_time = time.time()
        p_l = []
        for i in range(40):
            p = Thread(target=work2)
            p.start()
            p_l.append(p)
            
        for p in p_l:
            p.join()
    
        end_time = time.time()
        print('cost time:', end_time - start_time)
    

    死锁现象

    '''
    死锁现象(了解):
    
    '''
    from threading import Lock, Thread, current_thread
    import time
    
    mutex_a = Lock()
    mutex_b = Lock()
    #
    # print(id(mutex_a))
    # print(id(mutex_b))
    
    
    class MyThread(Thread):
    
        # 线程执行任务
        def run(self):
            self.func1()
            self.func2()
    
        def func1(self):
            mutex_a.acquire()
            # print(f'用户{current_thread().name}抢到锁a')
            print(f'用户{self.name}抢到锁a')
            mutex_b.acquire()
            print(f'用户{self.name}抢到锁b')
            mutex_b.release()
            print(f'用户{self.name}释放锁b')
            mutex_a.release()
            print(f'用户{self.name}释放锁a')
    
        def func2(self):
            mutex_b.acquire()
            print(f'用户{self.name}抢到锁b')
            # IO操作
            time.sleep(1)
    
            mutex_a.acquire()
            print(f'用户{self.name}抢到锁a')
            mutex_a.release()
            print(f'用户{self.name}释放锁a')
            mutex_b.release()
            print(f'用户{self.name}释放锁b')
    
    
    for line in range(10):
        t = MyThread()
        t.start()
    

    递归锁

    '''
    递归锁(了解):
        用于解决死锁问题.
    
    RLock: 比喻成万能钥匙,可以提供给多个人去使用.
        但是第一个使用的时候,会对该锁做一个引用计数.
        只有引用计数为0, 才能真正释放让另一个人去使用
    '''
    
    from threading import RLock, Thread, RLock
    import time
    
    mutex_a = mutex_b = RLock()
    
    
    class MyThread(Thread):
    
        # 线程执行任务
        def run(self):
            self.func1()
            self.func2()
    
        def func1(self):
            mutex_a.acquire()
            # print(f'用户{current_thread().name}抢到锁a')
            print(f'用户{self.name}抢到锁a')
            mutex_b.acquire()
            print(f'用户{self.name}抢到锁b')
            mutex_b.release()
            print(f'用户{self.name}释放锁b')
            mutex_a.release()
            print(f'用户{self.name}释放锁a')
    
        def func2(self):
            mutex_b.acquire()
            print(f'用户{self.name}抢到锁b')
            # IO操作
            time.sleep(1)
            mutex_a.acquire()
            print(f'用户{self.name}抢到锁a')
            mutex_a.release()
            print(f'用户{self.name}释放锁a')
            mutex_b.release()
            print(f'用户{self.name}释放锁b')
    
    
    for line in range(10):
        t = MyThread()
        t.start()
    

    信号量

    '''
    信号量(了解):
    
        互斥锁: 比喻成一个家用马桶.
            同一时间只能让一个人去使用
    
        信号量: 比喻成公厕多个马桶.
            同一时间可以让多个人去使用
    '''
    from threading import Semaphore, Lock
    from threading import current_thread
    from threading import Thread
    import time
    
    sm = Semaphore(5)  # 5个马桶
    mutex = Lock()  # 5个马桶
    
    
    def task():
        # mutex.acquire()
        sm.acquire()
        print(f'{current_thread().name}执行任务')
        time.sleep(1)
        sm.release()
        # mutex.release()
    
    
    for line in range(20):
        t = Thread(target=task)
        t.start()
    

    线程队列

    '''
    线程Q: 线程队列
    
        - FIFO队列: 先进先出
        - LIFO队列: 后进先出
        - 优先级队列: 根据参数分级
    '''
    import queue
    
    # 普通的线程队列: 先进先出
    # q = queue.Queue()
    # q.put(1)
    # q.put(2)
    # q.put(3)
    # print(q.get())  # 1
    
    
    # LIFO队列: 后进先出
    # q = queue.LifoQueue()
    # q.put(1)
    # q.put(2)
    # q.put(3)
    # print(q.get())  # 3
    
    
    # 优先级队列
    q = queue.PriorityQueue()  # 超级了解
    # 若参数中传的是元组,会以元组中第一个数字参数为准
    q.put(('a优', '先', '娃娃头', 4))  # a==97
    q.put(('a先', '优', '娃娃头', 3))  # a==98
    q.put(('a级', '级', '娃娃头', 2))  # a==99
    '''
    1.首先根据第一个参数判断ascii表的数值大小
    2.判断第个参数中的汉字顺序.
    3.再判断第二参数中数字--> 字符串数字 ---> 中文
    4.以此类推
    '''
    print(q.get())
    
  • 相关阅读:
    CVE-2020-5405 Spring Cloud Config 目录穿越漏洞分析
    CVE-2019-3799spring-cloud-config 目录穿越漏洞复现
    fastjson<=1.2.68的漏洞分析
    【转载】半自动化挖掘request实现多种中间件回显
    整理一下weblogic回显的代码
    CentOS7在python交互模式下输入退回键时出现乱码^H^H
    1 单例设计模式
    用nohup命令实现PHP的多进程
    用进程和线程关系引入操作系统学习
    8.5 归并排序
  • 原文地址:https://www.cnblogs.com/2222bai/p/11740377.html
Copyright © 2011-2022 走看看