What is the global interpreter lock (GIL) in CPython?

前端 未结 8 1913
悲哀的现实
悲哀的现实 2020-11-21 08:53

What is a global interpreter lock and why is it an issue?

A lot of noise has been made around removing the GIL from Python, and I\'d like to understand why that is s

相关标签:
8条回答
  • 2020-11-21 09:48

    Let's first understand what the python GIL provides:

    Any operation/instruction is executed in the interpreter. GIL ensures that interpreter is held by a single thread at a particular instant of time. And your python program with multiple threads works in a single interpreter. At any particular instant of time, this interpreter is held by a single thread. It means that only the thread which is holding the interpreter is running at any instant of time.

    Now why is that an issue:

    Your machine could be having multiple cores/processors. And multiple cores allow multiple threads to execute simultaneously i.e multiple threads could execute at any particular instant of time.. But since the interpreter is held by a single thread, other threads are not doing anything even though they have access to a core. So, you are not getting any advantage provided by multiple cores because at any instant only a single core, which is the core being used by the thread currently holding the interpreter, is being used. So, your program will take as long to execute as if it were a single threaded program.

    However, potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Taken from here. So for such operations, a multithreaded operation will still be faster than a single threaded operation despite the presence of GIL. So, GIL is not always a bottleneck.

    Edit: GIL is an implementation detail of CPython. IronPython and Jython don't have GIL, so a truly multithreaded program should be possible in them, thought I have never used PyPy and Jython and not sure of this.

    0 讨论(0)
  • 2020-11-21 09:49

    Whenever two threads have access to the same variable you have a problem. In C++ for instance, the way to avoid the problem is to define some mutex lock to prevent two thread to, let's say, enter the setter of an object at the same time.

    Multithreading is possible in python, but two threads cannot be executed at the same time at a granularity finer than one python instruction. The running thread is getting a global lock called GIL.

    This means if you begin write some multithreaded code in order to take advantage of your multicore processor, your performance won't improve. The usual workaround consists of going multiprocess.

    Note that it is possible to release the GIL if you're inside a method you wrote in C for instance.

    The use of a GIL is not inherent to Python but to some of its interpreter, including the most common CPython. (#edited, see comment)

    The GIL issue is still valid in Python 3000.

    0 讨论(0)
提交回复
热议问题