美文网首页
something about GIL

something about GIL

作者: 思而忧 | 来源:发表于2017-08-22 16:12 被阅读0次

文章全部摘至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

相关文章

  • something about GIL

    文章全部摘至Python官网上的python book里面。 GIL(Global Interpreter Loc...

  • About Something

    关于情绪 曾经我以为我是可以将情绪管理的很好的人,我以为将情绪深藏在心底不表现出来就是很好的自控力。然而,实际的情...

  • about something

    有人说“感情里人总要蠢一次” 最近我突然想明白了我和sxy的为什么会变成这样。那段时间大概是她最难受的时候,男朋友...

  • 2019-08-03

    Something about protocol of swift

  • Shallowly

    About youI am just saying Saying something about youAbout...

  • Diary-2.2

    There is no point of worrying about something uncertain ,...

  • test

    180718 something about fifa Halo, Ivan

  • Something About Movies

    Today we had our last lessons with two experienced Americ...

  • something about you

    人生苦短 我就不再想你了

  • Something about seniority in the

    By Cynthia In Chinese, seniority refers to the po...

网友评论

      本文标题:something about GIL

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