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
3
votes
1 answer

Coercion from Python not allowed without the GIL

I am trying to call a function from a C++ library defined like this: int somefunc(float timeout); I doubled a definition in the pxd file: int somefunc(float timeout) nogil; But any attempt to call the function with nogil leads to the same error…
Aleksei Petrenko
  • 6,698
  • 10
  • 53
  • 87
3
votes
0 answers

Which are the operations that are non-atomic with respect to the GIL?

I understand that from Python 3.2, the GIL works based on time of 5 miliseconds rather than 100 bytecodes before switching threads. However, if the operation needs a lock (needs to be atomic), then it waits for more than 5 milliseconds. I want to…
variable
  • 8,262
  • 9
  • 95
  • 215
3
votes
1 answer

Pybind11 function call blocking main thread

I'm using pybind11 to expose this game code (written in c++) to python. The first thing I'm trying to do is, basicly start the game by exposing a start function in the c++ part. So far is looking like this (c++): #define NOMINMAX #include…
3
votes
0 answers

How to get thread Trace in python GIL

In python how to get trace of thread interaction in GIL. t2 100 5351 ENTRY t2 100 5351 ACQUIRE t2 100 5352 RELEASE I have gone through the GIL PDF there i can see that traces of thread entry, acquire, release events. I want to know how we can get…
3
votes
1 answer

Threading a c++ program in python

I have a Python application written in Kivy that uses a C++ program for a high speed calculation, then it returns a value and my Python application uses that. The C++ program is wrapped in PyBind11 and imported into the application and then called…
ljelliot
  • 219
  • 2
  • 8
3
votes
1 answer

Releasing the GIL after destroying a sub-interpreter

I am embedding Python 3.2 in a C++ application and I have several sub interpreters that run at various times in the programs (created by Py_NewInterpreter). They acquire and release the GIL at various times, but I have run into a problem when I want…
Hal
  • 165
  • 1
  • 7
3
votes
0 answers

Python Vs C multithreading CPU utilization

I am toying with simple multi-threading program in both Python and C, where main starts multiple threads in a for loop and thread just do while(1). For both cases I ran 20 threads and observed CPU Utilization using 'top' with following…
HIq
  • 105
  • 6
3
votes
2 answers

Some standard C library math operations don't play nice with noGIL

I have a pyx file containing the following code: cimport cython from libc.math cimport sqrt, abs from libc.stdio cimport printf from cython.parallel import prange cdef double my_abs(double a): return sqrt(a*a) cpdef loop(int a): cdef int…
Takoda
  • 354
  • 3
  • 12
3
votes
1 answer

CherryPy 60x as slow in benchmark with 8 requesting threads compared to 7

I'm curious why when benchmarking Python web server CherryPy using ab, with -c 7 (7 concurrent threads) it can server 1500 requests/s (about what I expect), but when I change to -c 8 it drops way down to 25 requests/s. I'm running CherryPy with…
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
3
votes
0 answers

How to correctly finalize embedded Python interpreter using PyQt

I am trying to create a C++/Qt5 program that uses an embedded Python3 interpreter for scripting and customization purposes. This is my approach: I create a QApplication instance in C++, create a QMainwindow instance, and add a QVBoxLayout and a…
Arrahed
  • 101
  • 1
  • 5
3
votes
2 answers

Ordered status printing in multiprocessing

i have a program where i have to print to user after each successful step my program do, i have tried using Lock but it really slow down my program. Basically what i want to do is to print to user (ordered) each a succesful operation, like i have…
cojiko
  • 33
  • 3
3
votes
1 answer

Acquiring the global interpreter lock from python

Is it possible to acquire the global interpreter lock from python code? Or is that purely implemented in the C side?
Falmarri
  • 47,727
  • 41
  • 151
  • 191
3
votes
1 answer

Why/How python3 schedules threads in creation order?

The following code prints False on python27 and True on python36 on my Mac. from threading import Thread def make_huge_list(amount): my_list = [] def add_num(num): my_list.append(num) threads = [Thread(target=add_num,…
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
3
votes
0 answers

Does Matplotlib release the GIL while drawing with the Agg backend?

I would like to draw figures on multiple cores in parallel with Matplotlib using the Agg backend without having to start multiple Python processes. This could be achieved with multithreading if Matplotlib released the global interpreter lock (GIL)…
Agost Biro
  • 2,709
  • 1
  • 20
  • 33
3
votes
1 answer

Python multithreading and Global Interpreter Lock

I recently came across the GIL present in Python according to which only a single thread can execute at a time and multithreading can not utilize all the cores. Now in one of my projects, I used multithreading and lots of locks and semaphores So…