Questions tagged [gil]

Use for questions concerning CPython's Global Interpreter Lock

The Global Interpreter Lock (GIL) is a CPython implementation detail that ensures thread safety by disallowing concurrent execution of Python code. Python versions such as CPython (the Python reference implementation), Stackless Python and PyPy have the GIL; versions such as Jython and IronPython do not.

Python's Global Interpreter Lock (GIL) is a global mutex which ensures that only one thread of the interpreter can run Python bytecode at one time. It is the underlying mechanism that allows CPython as a whole to be thread-safe, despite parts of the interpreter such as garbage collection not being thread safe. While the GIL prevents concurrent threaded execution of Python code, code that waits for I/O like filesystem or database calls and code that does lengthy number-crunching in C (such as NumPy functions) will release the GIL to allow other threads to execute.

Versions of Python that run on virtual machines such as Jython and IronPython do not have a GIL, primarily because they can rely on the virtual machine to provide fully thread-safe garbage collection without reference counting.

The GIL has become the point of some contention as Python has evolved, because it constrains the performance of massively threaded programs, even on multi-CPU platforms which can otherwise execute code in parallel.

The defenders of the GIL (most notably GvR, the original creator of Python) argue that the GIL makes writing Python extensions simpler (because they can safely assume that only they are running), and that removing the GIL would necessitate more fine-grained mutexes that would impact single-threaded performance. The Python Wiki has more details on challenges with removing the GIL.

Various projects have aimed to remove the GIL, including:

  • A fork of Python from 1999, which died out due to poor single-threaded performance
  • Unladen Swallow
  • An experimental PyPy project to replace the GIL with transactional memory

Using Python on multiple cores is still possible using multiple processes (e.g. with the multiprocessing module), but communication between multiple processes is more difficult than communication between multiple threads on the same process. Also, spawning a process uses more CPU time and memory than spawning a thread.


More information on the GIL:

378 questions
2
votes
1 answer

Calling gil-requiring function not allowed without gil compilation error with C++

It is essentially an extension of this question - Usage of threadpoolexecutor in conjunction with cython's nogil In this case my getArea2() method is slightly different cdef int getArea2(self,double[:] p) nogil: cdef int area cdef…
gansub
  • 1,164
  • 3
  • 20
  • 47
2
votes
2 answers

400 threads in 20 processes outperform 400 threads in 4 processes while performing a CPU-bound task on 4 CPUs

This question is very similar to 400 threads in 20 processes outperform 400 threads in 4 processes while performing an I/O-bound task. The only difference is that the linked question is about an I/O-bound task whereas this question is about a…
Susam Pal
  • 32,765
  • 12
  • 81
  • 103
2
votes
0 answers

What are examples of what the GIL does? And how does it impact memory management when using multiple threads?

Cython's documentation talks about releasing the GIL however it does not explain what exactly the GIL does or allows. Similarly Python's documentation defines the GIL as: The mechanism used by the CPython interpreter to assure that only one thread…
Greg
  • 8,175
  • 16
  • 72
  • 125
2
votes
1 answer

Python 3.7 and PyGILState_Ensure() (Windows)

I have a Windows routine that uses the Python Library. I started out with 3.6.5 and a static library. Got it to work. Moved to a .dll Python library. Also works. Moved to 3.7. Does NOT work. It hangs when I attempt to close the embedded…
Jiminion
  • 5,080
  • 1
  • 31
  • 54
2
votes
2 answers

Why does a very small checkinterval not slow down CPU-intensive multithreading in python3?

from threading import Thread def countdown(start, end): while end > start: end -= 1 def multi_thread(n): t1 = Thread(target=countdown, args=(0, n // 2)) t2 = Thread(target=countdown, args=(n // 2, n)) t1.start() …
Jeffery
  • 307
  • 1
  • 10
2
votes
1 answer

What is the need of threading.Lock() when Cpython has GIL?

I am learning python language. I don't have a good understanding of threads and I can't think of a situation when you will need threading.Lock() as CPython's GIL only allows one thread to execute at a time so I think a critical section cannot be…
wings
  • 338
  • 4
  • 15
2
votes
0 answers

Calling python functions from MATLAB workers

I'm trying to run a MATLAB script using parfor which runs a simulink model in parallel by sim function, and this simulink model contains a MATLAB Function Block. The function defined in this block calls a python function using "py."…
2
votes
1 answer

Why is the Python interpreter not thread safe?

The GIL locks cores so that threads cannot run in parallel. Why is this the case? There is little information about this online.
2
votes
1 answer

Confusion about the term I/O when used in different settings and in reference to GIL

It seems when I google what I\O is, I get hits that show it stands for "Input" and "Output". I see blogs on Python titled, "Python File I\O --Part 19, Advanced File Input and Output " If I go to the Python docs and search for…
Moondra
  • 4,399
  • 9
  • 46
  • 104
2
votes
0 answers

Embedded Python(C++) Import Locks up?

So we are using a python interpreter embedded in our C++ application (using Py_initialize,etc) to run various test scripts. I am attempting to use the Pythonnet package. Running a normal python interpreter i can run import clr and have no issues and…
user1024792
  • 553
  • 1
  • 10
  • 23
2
votes
0 answers

How do you determine if a function holds the GIL?

Is there a reliable way to test whether a function holds (doesn't release) the GIL through its entire execution? If so, what is that method? For example, I want to determine if the key derivation algorithms from hashlib hold the GIL? I thought I…
Tankobot
  • 1,492
  • 14
  • 21
2
votes
2 answers

Is it possible to disable YARV's global interpreter lock?

This is more curiosity than anything else (I should totally drop that and try jRuby), but is it possible to disable YARV ruby's global interpreter lock and any other associated locks? I assume this would "void your warranty" as some of YARV Ruby's…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
2
votes
3 answers

Python Multithreading basics confusion

I have the below code: import time from threading import Thread def fun1(): time.sleep(5) def fun2(): time.sleep(5) def fun3(): time.sleep(5) def fun4(): time.sleep(5) if __name__ == '__main__': t1 = Thread(target=fun1, args=()) t2 =…
fsociety
  • 977
  • 3
  • 12
  • 23
2
votes
1 answer

pickle.dump blocks the main thread in multithreaded python application because of GIL

Currently I'm using python 3.4.3 and developing PyQt5 application. In my app, there's a QThread, and some large object(100MB) is being (pickle) dumped by the thread. However, dumping that object requires 1~2 seconds, and it blocks the main thread…
asqdf
  • 289
  • 2
  • 9
2
votes
1 answer

Is the thread created by ctypes also under GIL in python?

All python thread(in CPython) are under GIL. What if the thread is created by ctypes? For example, python just calls the below function through C Library and the function create a thread in C area not python. #include int…
SangminKim
  • 8,358
  • 14
  • 69
  • 125