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

Does GIL implementation became worse since Python3.2?

I have read great presentation about GIL: http://www.dabeaz.com/python/UnderstandingGIL.pdf And here Dave said, that since Python3.2 we have new and better version of GIL, and provide examples, that prove it (on slide 54). But when I test them on my…
Igor
  • 355
  • 2
  • 14
2
votes
1 answer

python multiple thread performance

I have one server with 10 cpu cores, when I ran the following code which one thread, one of the cpu cores usage was 100%: def fun(): while 1: pass but when I use 5 threads to run the same code, there were 5 cpu cores usages were: 30%,…
Jack
  • 5,540
  • 13
  • 65
  • 113
2
votes
1 answer

Plone: checkinterval can't import pystone

I found the hint about using checkinterval in the Plone documentation (for performance tuning) and tried to install it; in my buildout.cfg: [buildout] parts += checkinterval [checkinterval] recipe = zc.recipe.egg eggs = …
Tobias
  • 2,481
  • 3
  • 26
  • 38
2
votes
0 answers

How can I create a long GIL lock

I'm building a REST API in Python using Python 2.6, Flask on 64 Bit Linux. I've been asked to determine how the GIL could affect the performance of this service. What if something happened that caused the interpreter to lock for a few seconds?…
Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
2
votes
0 answers

Real-time output from multithreaded extensions in the IPython notebook

I have a c++ function that spawns a child thread to provide asynchronous output (eventually this will be used to implement a progress monitor reporting on the state of a multithreaded algorithm). I want the child thread to be able to output to any…
krcools
  • 897
  • 5
  • 12
2
votes
1 answer

Why is multiprocessing's apply_async so picky?

Sample code that works without issue: from multiprocessing import * import time import random def myfunc(d): a = random.randint(0,1000) d[a] = a print("Process; %s" % a) print("Starting mass threads") man = Manager() d = man.dict() p…
user1529891
2
votes
4 answers

Python: Plot some data (matplotlib) without GIL

my problem is the GIL of course. While I'm analysing data it would be nice to present some plots in between (so it's not too boring waiting for results) But the GIL prevents this (and this is bringing me to the point of asking myself if Python was…
BandGap
  • 1,745
  • 4
  • 19
  • 26
2
votes
1 answer

KMeans parallel processing failing

I'm running k-means on a big data set. I set it up like this: from sklearn.cluster import KMeans km = KMeans(n_clusters=500, max_iter = 1, n_init=1, init = 'random', precompute_distances = 0, n_jobs = -2) # The following line computes the fit…
zkurtz
  • 3,230
  • 7
  • 28
  • 64
2
votes
2 answers

Python & C/C++ multithreading : run several threads executing python in the background of C

I have a really specific need : I want to create a python console with a Qt Widget, and to be able to have several independent interpreters. Now let me try to explain where are my problems and all tries I did, in order of the ones I'd most like to…
hl037_
  • 3,520
  • 1
  • 27
  • 58
2
votes
1 answer

unable to release GIL with Cython when using callback-based C libraries

I'm creating a GTK/GStreamer application (C gstreamer for audio and pygobject gtk for gui) which waits for MIDI input and responds to it. The MIDI capturing is handled with the RTMidi library. In the past I was able to successfully release the GIL…
sugarmuff
  • 435
  • 5
  • 17
2
votes
1 answer

Does Pypy's stackless thread option support parallel execution?

I was reading about PyPy's stackless feature. My question is simple: does this get around the GIL? The page says it allows coding in "massively concurrent style". Does this also mean massively parallel style, taking advantage of multiple cores?
jmite
  • 8,171
  • 6
  • 40
  • 81
2
votes
3 answers

Is the new GIL in Python 3.2 sufficient to make the switch?

I was reading this page on the new GIL found/to be found in Python 3.2 and I was wondering if it is the "killer feature" that will trigger a transition from Python 2.x to 3.x. What do you guys think?
jldupont
  • 93,734
  • 56
  • 203
  • 318
2
votes
2 answers

Why do GIL alternatives have an impact on performance?

Coming from Java the whole Global Interpreter Lock (GIL) in Ruby and Python is kind of startling. I have read a bit into the problem and found in the Python documentation the following excerpt: Can’t we get rid of the Global Interpreter Lock? The…
Guarana Joe
  • 723
  • 1
  • 7
  • 16
2
votes
1 answer

Python GIL: Do all participants in an expression have ref count incremented for the duration of the expression?

Let's say I have a simple C++ class: class Widget { public: Widget() : x(1) { } void sleep() { sleep(x); } private: int x; }; Widget::sleep blocks, so I would like to free up the GIL so that Python can…
Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
2
votes
2 answers

Does mod_wsgi runs in a single python interpreter?

I have a django application which relies heavily on threading and I'm noticing no performance increment no matter how much processes or threads I add to the WSGIDaemonProcess. I can't find a YES/NO answer out there and I'm wondering. Could it be…
tutuca
  • 3,444
  • 6
  • 32
  • 54