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
276
votes
8 answers

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

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 so important. I have never written a compiler nor an interpreter myself, so don't be…
Bite code
  • 578,959
  • 113
  • 301
  • 329
188
votes
5 answers

Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?

I'm hoping someone can provide some insight as to what's fundamentally different about the Java Virtual Machine that allows it to implement threads nicely without the need for a Global Interpreter Lock (GIL), while Python necessitates such an evil.
AgentLiquid
  • 3,632
  • 7
  • 26
  • 30
57
votes
2 answers

Green-threads and thread in Python

As Wikipedia states: Green threads emulate multi-threaded environments without relying on any native OS capabilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread…
Rahul Gautam
  • 4,749
  • 2
  • 21
  • 30
37
votes
3 answers

numpy and Global Interpreter Lock

I am about to write some computationally-intensive Python code that'll almost certainly spend most of its time inside numpy's linear algebra functions. The problem at hand is embarrassingly parallel. Long story short, the easiest way for me to take…
NPE
  • 486,780
  • 108
  • 951
  • 1,012
28
votes
5 answers

Python Global Interpreter Lock (GIL) workaround on multi-core systems using taskset on Linux?

So I just finished watching this talk on the Python Global Interpreter Lock (GIL) http://blip.tv/file/2232410. The gist of it is that the GIL is a pretty good design for single core systems (Python essentially leaves the thread handling/scheduling…
user31056
24
votes
5 answers

Python performance - best parallelism approach

I am implementing a Python script that needs to keep sending 1500+ packets in parallel in less than 5 seconds each. In a nutshell what I need is: def send_pkts(ip): #craft packet while True: #send packet …
pascoal
  • 411
  • 2
  • 8
20
votes
1 answer

Why are numpy calculations not affected by the global interpreter lock?

I'm trying to decide if I should use multiprocessing or threading, and I've learned some interesting bits about the Global Interpreter Lock. In this nice blog post, it seems multithreading isn't suitable for busy tasks. However, I also learned that…
Lisa
  • 3,365
  • 3
  • 19
  • 30
20
votes
1 answer

Using a dictionary in Cython , especially inside nogil

I am having a dictionary, my_dict = {'a':[1,2,3], 'b':[4,5] , 'c':[7,1,2]) I want to use this dictionary inside a Cython nogil function . So , I tried to declare it as cdef dict cy_dict = my_dict Up to this stage is fine. Now I need to iterate…
Seja Nair
  • 787
  • 2
  • 9
  • 23
19
votes
4 answers

Embedding python in multithreaded C application

I'm embedding the python interpreter in a multithreaded C application and I'm a little confused as to what APIs I should use to ensure thread safety. From what I gathered, when embedding python it is up to the embedder to take care of the GIL lock…
shoosh
  • 76,898
  • 55
  • 205
  • 325
18
votes
1 answer

Can you race condition in Python while there is a GIL?

My understanding is that due to the Global Interpreter Lock (GIL) in cPython, only one thread can ever be executed at any one time. Does this or does this not automatically protected against race conditions, such as the lost update problem?
user2406944
17
votes
3 answers

Does using the subprocess module release the python GIL?

When calling a linux binary which takes a relatively long time through Python's subprocess module, does this release the GIL? I want to parallelise some code which calls a binary program from the command line. Is it better to use threads (through…
Simon Walker
  • 5,523
  • 6
  • 30
  • 32
16
votes
2 answers

sys.setswitchinterval in Python 3.2 and beyond

Python 3.2 introduced a new GIL implementation by Antoine Pitrou which exposes the function sys.setswitchinterval. When would changing this be useful, and why?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
16
votes
7 answers

GIL in Python 3.1

Does anybody knows fate of Global Interpreter Lock in Python 3.1 against C++ multithreading integration
Dewfy
  • 23,277
  • 13
  • 73
  • 121
16
votes
2 answers

How can I check whether a thread currently holds the GIL?

I tried to find a function that tells me whether the current thread has the global interpreter lock or not. The Python/C-API documentation does not seem to contain such a function. My current solution is to just acquire the lock using…
Adrian Genaid
  • 416
  • 5
  • 17
15
votes
1 answer

Where's the GIL in PyPy?

Is the PyPy GIL part of the PyPy interpreter implementation in RPython, or is it something that translate.py automatically adds? i.e., if I were to write my own new language interpreter in RPython and ran it through translate.py, would it be…
lobsterism
  • 3,469
  • 2
  • 22
  • 36
1
2 3
25 26