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
4
votes
2 answers

Running computation in background thread python/pygtk

Is there a way to run a python thread in the background without locking down the rest of python during time-consuming instructions? I'm trying to do time-consuming calculations in a background thread of a python (pygtk) application. I understand how…
4
votes
1 answer

How to call a multi-threaded C function in Cython?

I have a question about how to call a multi-threaded C function in Cython. Do I need to release/acquire the GIL before/after I do the multi-threaded stuff in the C function? Or can I just use it like a normal C function? Should I follow the…
Tianyang Li
  • 1,755
  • 5
  • 26
  • 42
4
votes
1 answer

Python GIL and multithreading

I would like to separate my sigle-thread application to number of working threads. Just 1 question - what about performance of this action? If GIL prevents python from executing more than 1 thread at the time will I have any profit? Another point…
Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
4
votes
3 answers

Python GIL: is django save() blocking?

My django app saves django models to a remote database. Sometimes the saves are bursty. In order to free the main thread (*thread_A*) of the application from the time toll of saving multiple objects to the database, I thought of transferring the…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
3
votes
2 answers

Why does Python's math.factorial not play nice with threads?

Why does math.factorial act so weird in a thread? Here is an example, it creates three threads: thread that just sleeps for a while thread that increments an int for a while thread that does math.factorial on a large number. It calls start on the…
W1N9Zr0
  • 133
  • 5
3
votes
1 answer

Can we run multi-threads in parallel in Ruby?

Please let me know if there is a way to run multi-threads in parallel. What I know till now is that Ruby has a global interpreter lock or global VM lock which blocks threads to run in parallel and implement concurrently. Please let me know some good…
3
votes
1 answer

Does PyNaCl release GIL and should it be used with multithreading

Does PyNaCl release the Global Interpreter Lock? Will it be suitable to use multithreading for encryption using PyNaCl's SecretBox? I want to encrypt relatively large amounts of data (~500 MB) using PyNaCl. For this, I divide it into chunks of…
kush
  • 154
  • 1
  • 12
3
votes
2 answers

python Global Interpreter Lock GIL problem

I want to provide a service on the web that people can test out the performance of an algo, which is written in python and running on the linux machine basically what I want to do is that, there is a very trivial PHP handler, let's say…
Ted Xu
  • 1,095
  • 1
  • 11
  • 20
3
votes
1 answer

How can I check if a thread holds the GIL with sub-interpreters?

I am working on some changes to a library which embeds Python which require me to utilize sub-interpreters in order to support resetting the python state, while avoiding calling Py_Finalize (since calling Py_Initialize afterwards is a no-no). I am…
Los Frijoles
  • 4,771
  • 5
  • 30
  • 49
3
votes
2 answers

Parallel threading python GIL vs Java

I know that python has a GIL that make threads not be able to run at the same time therefore threading is just context switching. Why is java different? Threads on the same CPU in every language cannot run parallel. Is creating new thread in java…
Amit Neuhaus
  • 183
  • 3
  • 14
3
votes
4 answers

Python GIL and threads synchronization

After having read various articles that explain GIS and threads in Python, and Are locks unnecessary in multi-threaded Python code because of the GIL? which is a very useful answer, I have one "last question". If, ideally, my threads only operate on…
giohappy
  • 520
  • 1
  • 5
  • 17
3
votes
2 answers

Do file I/O operations release the GIL in Python?

Based on what I have read - for example here - I understand the I/O operations release the GIL. So, if I have to read a large number of files on the local filesystem, my understanding is that a threaded execution should speed things up. To test this…
Mortz
  • 4,654
  • 1
  • 19
  • 35
3
votes
2 answers

Can I apply multithreading for computationally intensive task in python?

Update: To save your time, I give the answer directly here. Python can not utilize multi cpu cores at the same time if the you use pure Python to write your code. But Python can utilize multi cores at the same time when it calls some functions or…
Jingnan Jia
  • 1,108
  • 2
  • 12
  • 28
3
votes
1 answer

Reference counting of memoryviews with nogil

I don't quite understand how reference counting is done with memoryviews in large/longer nogil sections. Let's assume basically all my code is nogil, except for the creation of a numpy-array-to-memoryview deep down. The memoryview is returned and…
oli
  • 659
  • 1
  • 6
  • 18
3
votes
1 answer

Why is the 'append' method of Python 'list' atomic while i = i + 1 is not atomic?

According to the Python Documentation, the append operation on Python list is atomic. At the same time addition operation is not atomic: i = i + 1 I understand that the Python GIL is enforcing the append operation to be atomic. My question is why…
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127