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

Understanding preemptive multitasking with locks and the Python GIL?

I'm reading through Grok The GIL and it has the following statement in the discussion about locking. So long as no thread holds a lock while it sleeps, does I/O, or some other GIL-dropping operation, you should use the coarsest, simplest locks…
Josh Russo
  • 3,080
  • 2
  • 41
  • 62
3
votes
1 answer

Global Interpreter lock: Jython vs CPython

CPython uses GIL to prevent problems such as mutual exclusion. However, the consequence is that the interpreter is not able to take advantage of a multi-core CPU. I also learnt that Jython does not require a GIL because its implementation is already…
lpan
  • 448
  • 5
  • 10
3
votes
0 answers

using numpy array of arrays in Cython parallel with nogil

I want to assign numpy array of arrays (array with elements as array) inside Cython parallel loop with nogil. I use the dtype as an object to be able to assign the numpy array as an array element. It works in Cython without parallelizing. But by…
Haleh
  • 31
  • 3
3
votes
0 answers

How such code executes in parallel despite GIL?

It's all about Python2.7 I have next question: from futures import ThreadPoolExecutor def test(): while True: i = 4 executor = ThreadPoolExecutor(max_workers=2) executor.submit(test) executor.submit(test) So when I run such code…
Igor
  • 355
  • 2
  • 14
3
votes
1 answer

Python Threading Performance vs. Number of cores

I am new to python and I am learning threading and GIL. These are the stats oflscpu command : Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s)…
2rd_7
  • 462
  • 1
  • 3
  • 15
3
votes
2 answers

Python GIL prevent CPU usage to exceed 100% in multiple core machine?

Many references say that, Python GIL lower down the performance of multi threading code in multi core machine, since each thread will need to acquire the GIL before executioin. In other words, it looks like GIL make a multi threading Python program…
twds
  • 333
  • 1
  • 4
  • 15
3
votes
2 answers

Will I run into trouble with python's Global Interpreter Lock?

I am aware that this question is rather high-level and may be vague. Please ask if you need any more details and I will try to edit. I am using QuickFix with Python bindings to consume high-throughput market data from circa 30 markets…
Wapiti
  • 1,851
  • 2
  • 20
  • 40
3
votes
0 answers

How to wrap a message loop with callbacks using boost::python (keeping the GIL in mind)

I want to wrap an existing C++ library which involves a blocking message loop and calling handler functions for Python using boost::python. E.g.: import my_boostpython_lib def my_handler_fn(): do_something() md =…
frans
  • 8,868
  • 11
  • 58
  • 132
3
votes
0 answers

Repeated vs. Intermittent use of the GIL

Have there been any comparisons done on acquiring/releasing the GIL frequently vs. sparingly? For example, take the keywdarg_parrot function defined here. If a developer wanted to be cognizant of the other threads that may require the GIL, they…
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
3
votes
1 answer

call multiple c++ functions in python using threads

Suppose I have a C(++) function taking an integer, and it is bound to (C)python with python api, so I can call it from python: import c_module c_module.f(10) now, I want to parallelize it. The problem is: how does the GIL work in this case? Suppose…
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
3
votes
2 answers

PyQt, QThread, GIL, GUI

I have GUI and program logic written in Python. I am requesting information from web by calling urllib.requests (and so on) very often and this cause a problem when GUI is unresponsive but this calls are wrapped with QThread. I think that happens…
VP.
  • 15,509
  • 17
  • 91
  • 161
3
votes
1 answer

Understanding python GIL - I/O bound vs CPU bound

From python threading documentation In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your …
PrivateUser
  • 4,474
  • 12
  • 61
  • 94
3
votes
1 answer

OpenMP, Python, C Extension, Memory Access and the evil GIL

so I am currently trying to do something like A**b for some 2d ndarray and a double b in parallel for Python. I would like to do it with a C extension using OpenMP (yes I know, there is Cython etc. but at some point I always ran into trouble with…
Jack
  • 195
  • 2
  • 10
3
votes
4 answers

A question on python GIL

Does the presence of python GIL imply that in python multi threading the same operation is not so different from repeating it in a single thread?. For example, If I need to upload two files, what is the advantage of doing them in two threads instead…
asdfg
  • 2,541
  • 2
  • 26
  • 25
3
votes
1 answer

Is there a general purpose way to test for the existence of a GIL?

I'm writing a multi-threaded Python application, which will behave differently on systems, depending on the details of the GIL implementation. Is there a general purpose way for me to test whether the intepreter I'm running on has a CPython style…
blueberryfields
  • 45,910
  • 28
  • 89
  • 168