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
6
votes
5 answers

running multiple threads in python, simultaneously - is it possible?

I'm writing a little crawler that should fetch a URL multiple times, I want all of the threads to run at the same time (simultaneously). I've written a little piece of code that should do that. import thread from urllib2 import Request, urlopen,…
YSY
  • 1,226
  • 3
  • 13
  • 19
6
votes
2 answers

How to invoke Python function as a callback inside C++ thread using pybind11

I designed a C++ system that invokes user defined callbacks from procedure running in a separate thread. Simplified system.hpp looks like this: #pragma once #include #include #include #include class…
pptaszni
  • 5,591
  • 5
  • 27
  • 43
6
votes
2 answers

Does new implementation of GIL in Python handled race condition issue?

I've read an article about multithreading in Python where they trying to use Synchronization to solve race condition issue. And I've run the example code below to reproduce race condition issue: import threading # global variable x x = 0 def…
Toan Quoc Ho
  • 3,266
  • 1
  • 14
  • 22
6
votes
1 answer

Has the Convoy Effect from Python's GIL been solved since Python 3.2?

I am investigating how Python's GIL works. I'm learning from the slides below, but I have one question. http://www.dabeaz.com/python/UnderstandingGIL.pdf This slide describes a new GIL from Python 3.2 and provides an overview. Among them, as a…
ttiger
  • 141
  • 1
  • 5
6
votes
1 answer

How to troubleshoot/bypass locking problem which appears to be GIL related

An obscure lock-up between two threads seems to be related to the Global Interpreter Lock or some other "behind-the-scenes-lock", and I am at a loss how to continue troubleshooting. Any tips how to eliminate the lock-up would be appreciated. The…
CptJeanLuc
  • 141
  • 1
  • 5
6
votes
0 answers

Can I const-access CPython objects without GIL?

Context and motivation This is a question about releasing GIL in CPython while still working with Python objects, but in a limited way. For performance reasons, I need to jump into GIL-less mode while maintaining read-only access to existing Python…
cython_q
  • 61
  • 4
6
votes
4 answers

Python threading and GIL

I was reading about the GIL and it never really specified if this includes the main thread or not (i assume so). Reason I ask is because I have a program with threads setup that modify a dictionary. The main thread adds/deletes based on player input…
Charles
  • 61
  • 1
  • 2
6
votes
1 answer

A multi-threading example of the python GIL

I've read a quite a bit about how "bad" this python GIL business is when writing multi-threaded code, but I've never seen an example. Could someone please give me a basic example of when the GIL causes problems when using threading. Thanks!
vgoklani
  • 10,685
  • 16
  • 63
  • 101
6
votes
1 answer

boost.python c++ multithreading

I'm writing a python program that includes a c++ module (.so, using boost.python). I'm starting several python threads that run a c++ function. This is how the C++ code looks like: #include using namespace boost; void f(){ //…
Gal Malka
  • 307
  • 3
  • 12
6
votes
2 answers

Python requires a GIL. But Jython & IronPython don't. Why?

Why is it that you can run Jython and IronPython without the need for a GIL but Python (CPython) requires a GIL?
JamesD
  • 77
  • 1
  • 3
6
votes
1 answer

Python threads and GIL

Let's say I have a thread and the main part of the program. Because of GIL, one thread should work at a time right (and not simulatenously)? But what if One of the threads is an infinite loop (or both for that matter)? Would these two processes run…
Luis Cruz
  • 1,488
  • 3
  • 22
  • 50
6
votes
1 answer

How could I use GIL for a dictionary in a multithreaded application?

I have encountered error 'RuntimeError: dictionary changed size during iteration' while iterating through a dictionary in a thread,which is being inserted in another thread in Python 2.7.I found that by using Global Intrepreter Lock,we could lock a…
titto.sebastian
  • 511
  • 2
  • 7
  • 17
6
votes
1 answer

Python GIL and threads

I have embedded Python3 in my big C++ application. Python gives the user script capability for custom data processing. Problem : I have many threads that interact with Python and I don't really get how to protect my code with GIL. So far, the only…
poukill
  • 540
  • 8
  • 18
6
votes
1 answer

Trying to parallelize a python algorithm using multithreading and avoiding GIL restrictions

I am implementing an algorithm in Python using Biopython. I have several alignments (sets of sequences of equal length) stored in FASTA files. Each alignment contains between 500 and 30000 seqs and each sequence is about 17000 elements long. Each…
5
votes
2 answers

Is asyncio affected by the GIL?

On this page I read this: Coroutines in the asyncio module are not limited by the Global Interpreter Lock or GIL. But how is this possible if both the asyncio event loop and the threading threads are running in a single Python process with GIL? As…
Meetinger
  • 165
  • 8