一、Gil

作者: 薛定谔的猫_1406 | 来源:发表于2018-04-12 11:09 被阅读0次
#gil global interpreter lock (cpython)
#python中一个线程对应于c语言中的一个线程
#gil使得同一个时刻只有一个线程在一个cpu上执行字节码, 无法将多个线程映射到多个cpu上执行

#gil会根据执行的字节码行数以及时间片释放gil,gil在遇到io的操作时候主动释放
# import dis
# def add(a):
#     a = a+1
#     return a
# 
# print(dis.dis(add))

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)

相关文章

  • 008 12. python提高-1

    12.1. GIL GIL(全局解释器锁) GIL面试题如下 描述Python GIL的概念, 以及它对pytho...

  • 一、Gil

  • python之理解GIL

    python之理解GIL 1、GIL简介 GIL的全称为Global Interpreter Lock,全局解释器...

  • GIL下的多线程

    1. GIL GIL 是什么? GIL 全称 Global Interpreter Lock,全局解释锁。存在于 ...

  • Python中的GIL锁

    1、什么是GIL锁: GIL的全称是Global Interpreter Lock(全局解释器锁),GIL...

  • GIL

    一,GIL的概念 python全局解释器锁。 二,GIL产生的原因 GIL 并不是 Python 语言的特性,它是...

  • Python高级语法1:GIL锁&浅拷贝&深拷贝

    一、GIL锁 1.1、GIL面试题:描述Python GIL的概念, 以及它对python多线程的影响?编写一个多...

  • Python GIL

    一、Python GIL ==GIL== :全局解释器锁。每个线程在执行的过程都需要先获取GIL,保证同一时刻只有...

  • Python day28_GIL 深拷贝浅拷贝

    GIL(全局解释器锁) GIL面试题如下 描述Python GIL的概念, 以及它对python多线程的影响?编写...

  • 线程安全,GIL全局锁

    1. GIL是什么? GIL不是Python特性GIL是实现Python解释器(Cpython)时引入的概念,而C...

网友评论

      本文标题:一、Gil

      本文链接:https://www.haomeiwen.com/subject/sdrxkftx.html