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

A conceptual question on python GIL, what does it actually mean

I suspect similar questions might have been asked multiple times. But it's hard for me to get an answer for my question.. I understand GIL makes only single python thread can execute at a time. (My understanding comes from…
eugene
  • 39,839
  • 68
  • 255
  • 489
2
votes
1 answer

Does the pythonic os.sched_yield() release the GIL?

I've found this question about GIL released by modules: Does who releases GIL in Python? I was looking for the os.sched_yield() implementation and I can't see the GIL release macro or function: /*[clinic input] os.sched_yield Voluntarily relinquish…
DeZee
  • 95
  • 6
2
votes
1 answer

Which functions release the GIL in Python?

I found this sentence about GIL on the Python wiki: Luckily, many potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Is there a list of functions outside the GIL (at…
Kang San Lee
  • 312
  • 2
  • 10
2
votes
1 answer

Is there a way to make something happen if somebody takes too long to answer an input on Python?

For example if you took over 10 seconds to answer an input the program would print text or execute some line of code. You could still answer, just text would be printing while you haven't answered, or something similar to that.
2
votes
0 answers

Why does python not lock only the mutable data?

In Python, only one thread can interact with the Interpreter. The GIL enforces this mechanism. This is to ensure thread safety. What are the reasons to not have one lock per mutable data and this lock has to be acquired to read/write to/from this…
figs_and_nuts
  • 4,870
  • 2
  • 31
  • 56
2
votes
1 answer

Is there a way to perform a deep copy of a PyObject without using the Python API (e.g. via C, Rust, etc)?

I was wondering if anyone knew of an implementation/library I could use to perform a deep copy of a PyObject without using the Python API. I'd prefer something in C (as I currently use, and am somewhat familiar with CFFI), but anything (no matter…
BL12345
  • 39
  • 3
2
votes
0 answers

Cython: copy memoryview without gil

I have the following code section (appropriately simplified) cpdef double func(double[:] x, double[:] y) nogil: cdef: double[:] _y _y = y # Here's my trouble _y[2] = 2. - y[1] _y[1] = 1. return func2(x, _y) I'm trying to…
Faydey
  • 725
  • 1
  • 5
  • 12
2
votes
2 answers

Would limiting a GILed Python program to a single CPU boost performance?

Following up on David Beazley's paper regarding Python and GIL, would it be a good practice to limit a Python program (CPython with GIL and all) to a single CPU in a Windows based multi-core system? Would it boost performance? UPDATE: Assume…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
2
votes
1 answer

How Puma allows IO waiting to be done in parallel to improve MRI's throughput?

From Puma's README: On MRI, there is a Global VM Lock (GVL) that ensures only one thread can run Ruby code at a time. But if you're doing a lot of blocking IO (such as HTTP calls to external APIs like Twitter), Puma still improves MRI's throughput…
Weihang Jian
  • 7,826
  • 4
  • 44
  • 55
2
votes
1 answer

Could Python decode JSON in parallel threads?

In a world of APIs, it seems many Python apps spend more time decoding JSON than anything else. It seems that all JSON decoders however lock the GIL while decoding, meaning it is not possible to speed up JSON decoding using multi-threading, or in…
aaa90210
  • 11,295
  • 13
  • 51
  • 88
2
votes
0 answers

Joblib Shared Memory With nogil

We know that python has a global interpreter lock that can incur a lot of thread contention if we use a threading backend in a Parallel() task in Joblib. However, suppose I made a Cython script: cdef map[string, int] very_large_map cdef map[string,…
Caprikuarius
  • 176
  • 7
2
votes
0 answers

GIL Deadlock when thread raise an exception

if a thread raises an exception, would it not release the GIL and cause deadlock?
kira
  • 31
  • 4
2
votes
2 answers

Does Multithreading work on Google Cloud CPU instances?

A friend said he was able to get multithreading in python (with Anaconda's default interpreter, probably cpython?) to utilise all cores on Google Cloud vCPUs. However, ordinarily, multithreading in python is limited to a single core on local…
Raskell
  • 158
  • 3
  • 9
2
votes
0 answers

How to bypass the GIL and handle the KeyboardInterrupt?

I'm wanting to exit an UDP-server on KeyboardInterrupt, but this is not working immediately. when doing some research I stumbled on this question, where someone suggests that it is a issue with the GIL. Python processes calling into C APIs can block…
Dries Jans
  • 107
  • 1
  • 11
2
votes
0 answers

Embedding Python in a C++ program, how to deal with the GIL

At work, we start an embedded Python interpreter from within a C++ program in firmware. The following program shows the essence of how the interpreter is run. In reality this starts a large program, but in this example a short command that…
doetoe
  • 765
  • 7
  • 16