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

Is concurrent.futures a medicine of the GIL?

I was just searching about this new implementation, and i use python 2.7, i must install this, so if i use it, i'll forget the word GIL on CPython?
Abdelouahab Pp
  • 4,252
  • 11
  • 42
  • 65
12
votes
1 answer

Where can I find a list of numpy functions which release the GIL?

I have found several SO questions asking about this in one way or another, but none of them actually either give a list or refer to one. This question refers to a wiki page, but while the wiki page talks about the GIL and multi-threading, it doesn't…
DanielSank
  • 3,303
  • 3
  • 24
  • 42
12
votes
3 answers

What does Python's GIL have to do with the garbage collector?

I just saw this section of Unladen Swallow's documentation come up on Hacker News. Basically, it's the Google engineers saying that they're not optimistic about removing the GIL. However, it seems as though there is discussion about the garbage…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
12
votes
3 answers

Is the Python GIL really per interpreter?

I often see people talking that the GIL is per Python Interpreter (even here on stackoverflow). But what I see in the source code it seems to be that the GIL is a global variable and therefore there is one GIL for all Interpreters in each python…
Lothar
  • 12,537
  • 6
  • 72
  • 121
11
votes
4 answers

When are Python threads fast?

We're all aware of the horrors of the GIL, and I've seen a lot of discussion about the right time to use the multiprocessing module, but I still don't feel that I have a good intuition about when threading in Python (focusing mainly on CPython) is…
mvanveen
  • 9,754
  • 8
  • 33
  • 42
11
votes
1 answer

Is there a way to release the GIL for pure functions using pure python?

I think I must be missing something; this seems so right, but I can't see a way to do this. Say you have a pure function in Python: from math import sin, cos def f(t): x = 16 * sin(t) ** 3 y = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) -…
FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32
11
votes
2 answers

Why Do I have to worry about Thread Safety in CPython?

From what I understand, the Global Interpreter Lock allows only a single thread to access the interpreter and execute bytecode. If that's the case, then at any given time, only a single thread will be using the interpreter and its memory. With that…
Anfernee
  • 1,445
  • 1
  • 15
  • 26
10
votes
1 answer

Handling GIL when calling python lambda from C++ function

The question Is pybind11 somehow magically doing the work of PyGILState_Ensure() and PyGILState_Release()? And if not, how should I do it? More details There are many questions regarding passing a python function to C++ as a callback using pybind11,…
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
10
votes
2 answers

Why is ThreadPoolExecutor's default max_workers decided based on the number of CPUs?

The documentation for concurrent.futures.ThreadPoolExecutor says: Changed in version 3.5: If max_workers is None or not given, it will default to the number of processors on the machine, multiplied by 5, assuming that ThreadPoolExecutor is often…
Lone Learner
  • 18,088
  • 20
  • 102
  • 200
10
votes
6 answers

What is C#'s version of the GIL?

In the current implementation of CPython, there is an object known as the "GIL" or "Global Interpreter Lock". It is essentially a mutex that prevents two Python threads from executing Python code at the same time. This prevents two threads from…
Thanatos
  • 42,585
  • 14
  • 91
  • 146
10
votes
2 answers

Why does multithreading do not speed up parsing HTML with lxml?

I am trying to understand why running multiple parsers in parallel threads does not speed up parsing HTML. One thread does 100 tasks twice as fast as two threads with 50 tasks each. Here is my code: from lxml.html import fromstring import time from…
10
votes
5 answers

Does a multithreaded crawler in Python really speed things up?

Was looking to write a little web crawler in python. I was starting to investigate writing it as a multithreaded script, one pool of threads downloading and one pool processing results. Due to the GIL would it actually do simultaneous downloading?…
James
  • 15,085
  • 25
  • 83
  • 120
10
votes
2 answers

Why does Python provide locking mechanisms if it's subject to a GIL?

I'm aware that Python threads can only execute bytecode one at a time, so why would the threading library provide locks? I'm assuming race conditions can't occur if only one thread is executing at a time. The library provides locks, conditions, and…
danihodovic
  • 1,151
  • 3
  • 18
  • 28
9
votes
4 answers

What's the cost of releasing the GIL?

Let's suppose I have a C extension function that does something that is completely independent of the Python interpreter. Is there any reason not to release the GIL? For example, is there any reason not to write code like this (apart from issues…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
9
votes
5 answers

Multiprocessing useless with urllib2?

I recently tried to speed up a little tool (which uses urllib2 to send a request to the (unofficial)twitter-button-count-url (> 2000 urls) and parses it´s results) with the multiprocessing module (and it´s worker pools). I read several discussion…
dorvak
  • 9,219
  • 4
  • 34
  • 43
1
2
3
25 26