something about GIL

文章全部摘至Python官网上的python book里面。

GIL(Global Interpreter Lock)中文意思不翻译了,意思还是留给读的人去体会。
官方上的解析

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.)

一般讨论GIL是在CPython下的,如果使用其他环境下的解析器就可能不会有这种限制,这跟系统的设计有关。GIL的设计是为了线程安全,保护内存数据。

关于网络上所说的瓶颈和性能问题

官网上的说法:

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

关于一些性能上限制主要是说密集计算型的程序,对于一些不用处理复杂计算的任务,像IO读写这些并不在GIL范围里面,所以密集的IO读写即使在GIL环境下也是能多线程去读写操作并且有实际意义。

However the GIL can degrade performance even when it is not a bottleneck. Summarizing those slides: The system call overhead is significant, especially on multicore hardware. Two threads calling a function may take twice as much time as a single thread calling the function twice. The GIL can cause I/O-bound threads to be scheduled ahead of CPU-bound threads. And it prevents signals from being delivered.

创建两个线程运行同一个函数是比单线程情况下调用同一个函数两次消耗的时间多,GIL同时只能执行一个线程,线程之间的调度消耗更多的时间。

Therefore, the rule exists that only the thread that has acquired the GIL may operate on Python objects or call Python/C API functions. In order to emulate concurrency of execution, the interpreter regularly tries to switch threads (see sys.setswitchinterval()). The lock is also released around potentially blocking I/O operations like reading or writing a file, so that other Python threads can run in the meantime.

对于一些io密集型程序,python的多线程是有实际效果的,文件读写和下载等等操作

Non-CPython implementations

  • Jython and IronPython have no GIL and can fully exploit multiprocessor systems
  • PyPy currently has a GIL like CPython
  • in Cython the GIL exists, but can be released temporarily using a "with" statement
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 引言&动机 考虑一下这个场景,我们有10000条数据需要处理,处理每条数据需要花费1秒,但读取数据只需要0.1秒,...
    chen_000阅读 528评论 0 0
  • 前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上...
    LazzMan阅读 569评论 0 1
  • 灯红酒绿中,人与人变的越来越冷艳……除了利益争斗,剩下那几颗万分可贵的初心碰撞,叫人无比珍惜! 纷扰中,能得...
    任性笔头阅读 351评论 0 0
  • 有一个八岁的女孩,名叫金晶花;就是我,有一次我跟姐姐打架了,两个人不知因什么起了争执,你一拳我一拳的打了起来,大家...
    金晶花阅读 283评论 3 0
  • 开始创业已有小半年。本人广告行业出身,头脑一热扎进服装行业搞起了服装电商,做起了自己的服装品牌sheer白T,只...
    享受谎言无冕之王阅读 486评论 0 0