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

Understanding performance impact on a concurrent server because of GIL

I am bit confused with what is happening with my code below. I have created a concurrent server to find nth number in fibonacci series. Server creates a thread for each client connection and thread delegates calculation of finding fibonacci number…
ThinkGeek
  • 4,749
  • 13
  • 44
  • 91
4
votes
2 answers

is it possible to release the GIL before a C function that blocks and might call back into Python?

I am wrapping a C function which performs a blocking operation (select) and then handles incoming messages. My understanding is that when a C function is going to block, the correct way to call it while allowing other threads to run…
Steve
  • 8,153
  • 9
  • 44
  • 91
4
votes
1 answer

How to convert Python threading code to multiprocessing code?

I need to convert a threading application to a multiprocessing application for multiple reasons (GIL, memory leaks). Fortunately the threads are quite isolated and only communicate via Queue.Queues. This primitive is also available in…
Helmut Grohne
  • 6,578
  • 2
  • 31
  • 67
4
votes
2 answers

Simple Python function that holds the GIL

I would like to test how my application responds to functions that hold the GIL. Is there a convenient function that holds the GIL for a predictable (or even a significant) amount of time? My ideal function would be something that operated like…
MRocklin
  • 55,641
  • 23
  • 163
  • 235
4
votes
2 answers

True parallelism in Python

Is it possible to have true parallelism in python due to the presence of GIL? As far as I know each thread acquires the GIL before executing while other threads wait until the GIL is released. Why have GIL if it is such a bottleneck
ffff
  • 2,853
  • 1
  • 25
  • 44
4
votes
2 answers

Does Python GIL need to be taken care when work with multi-thread C++ extension?

I'm now implementing a data subscriber with Python, which subscribes to a data publisher (actually a ZeroMQ publisher socket) and will get notified once any new messages are fed. In my subscriber, the messages are dumped to a data processor after…
Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83
4
votes
5 answers

Looking for strong/explicit-typed language without GIL

Are there any languages which feature static type checking like in C++ with modern syntax like in Python, and does not have GIL? I belive, Python 3 with ability to explicitly declare type of each variable would be 'almost there', but GIL makes me…
BarsMonster
  • 6,483
  • 2
  • 34
  • 47
4
votes
1 answer

Do coroutines from PEP-492 bypass the GIL in Python 3.5?

Python 3.5 includes co-routine support with PEP-492; which is great and all.... assuming the coroutines go around the GIL; does anyone know if that's the case? Or, should I just keep using the multiprocessing module?
user1529891
4
votes
1 answer

Do scipy.sparse functions release the GIL?

Question Do scipy.sparse functions, like csr._mul_matvec release the GIL? Context Python functions that wrap foreign code (like C) often release the GIL during execution, enabling parallelism with multi-threading. This is common in the numpy…
MRocklin
  • 55,641
  • 23
  • 163
  • 235
4
votes
3 answers

Multiple processes with multiple threads in Python

I've heard something about "If you want to get maximum performance from parallel application, you should create as many processes as your computer has CPUs, and in each process -- create some (how many?) threads". Is it true? I wrote a piece of code…
vortexxx192
  • 929
  • 1
  • 9
  • 24
4
votes
2 answers

Python GIL and globals

In python, I have a global variable defined that gets read/incremented by different threads. Because of the GIL, will this ever cause problems without using any kind of locking mechanism?
4
votes
1 answer

f2py function release GIL

Does the Global Interpretter Lock (GIL) get released when I call an f2py wrapped function? (I'm happy to try to discover on my own, but I'm not familiar enough with the numpy source to know where to start looking)... To clarify, a good answer to…
mgilson
  • 300,191
  • 65
  • 633
  • 696
4
votes
1 answer

Force the GIL not to switch threads

I'm profiling some multi-threaded CPython code. In order to measure the time it takes to execute a specific code segment, I would like to force the GIL (Global Interpreter Lock) to not switch between threads for that segment. How can this be…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
4
votes
1 answer

A thread-safe memoize decorator

I'm trying to make a memoize decorator that works with multiple threads. I understood that I need to use the cache as a shared object between the threads, and acquire/lock the shared object. I'm of course launching the threads: for i in range(5): …
iTayb
  • 12,373
  • 24
  • 81
  • 135
4
votes
2 answers

file.read() multiprocessing and the GIL

I've read that certain Python functions implemented in C, which I assume includes file.read(), can release the GIL while they're working and then get it back on completion and by doing so make use of multiple cores if they're available. I'm using…
redrah
  • 1,204
  • 11
  • 20