zoukankan      html  css  js  c++  java
  • python GIL

    GIL: Global Interpreter Lock,相当于一个锁

    python中一个线程对应于c语言中的一个线程,gil使得同一个时刻只有一个线程在一个cpu上执行字节码, 无法将多个线程映射到多个cpu(一个CPU一核)或多核CPU上执行,gil会根据执行的字节码行数以及时间片释放gil,gil在遇到io的操作时候主动释放

    total = 0
    
    def add():
        #1. dosomething1
        #2. io操作
        # 1. dosomething3
        global total
        for i in range(1000000):
            total += 1
    def desc():
        global total
        for i in range(1000000):
            total -= 1
    
    import threading
    thread1 = threading.Thread(target=add)
    thread2 = threading.Thread(target=desc)
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()
    print(total)
  • 相关阅读:
    软件工程实践总结
    用户使用调查报告
    Beta 冲刺 随笔合集
    Beta 冲刺 七
    Beta 冲刺 六
    Beta 冲刺 五
    Beta 冲刺 四
    Beta 冲刺 三
    Beta 冲刺 二
    Beta 冲刺 一
  • 原文地址:https://www.cnblogs.com/callyblog/p/11129336.html
Copyright © 2011-2022 走看看