阅读背景:

python GIL全局解释器锁,多线程多进程效率比较,协程

来源:互联网 

GIL全局解释器锁

'''
python解释器:
    - Cpython C语言
    - Jpython  java
    ...

1、GIL: 全局解释器锁
    - 翻译: 在同一个进程下开启的多线程,同一时刻只能有一个线程执行,因为Cpython的内存管理不是线程安全。

    - 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解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势。

# 为什么要有全局解释器锁:
    - 没有锁:

2、GIL全局解释器锁的优缺点:
    1.优点:
        保证数据的安全
    2.缺点:
        单个进程下,开启多个线程,牺牲执行效率,无法实现并行,只能实现并发。

        - IO密集型, 多线程
        - 计算密集型,多进程

python解释器:
    - Cpython C语言



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: