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

Processing a list of lists in Cython, with nogil

In Python I have a list of lists as input: input = [[0,1,2],[0,3,4,5],[0,6]] In reality, the number of sub-lists is tens of thousands. The length of each sub-list could vary greatly, from zero or one value to many hundreds. I want to pass the input…
matthiash
  • 3,105
  • 3
  • 23
  • 34
5
votes
3 answers

Profiling the GIL

Is there a way to profile a python process' usage of the GIL? Basically, I want to find out what percentage of the time the GIL is held. The process is single-threaded. My motivation is that I have some code written in Cython, which uses nogil. …
shx2
  • 61,779
  • 13
  • 130
  • 153
5
votes
1 answer

How does the GIL in python affect the downloading of webpages in parallel?

My understanding of the background of this question: The GIL limits python to one thread running at a time. Because of the GIL, multithreading long calculations is not useful Threading can still be useful Threading may be useful with I/O…
ADJenks
  • 2,973
  • 27
  • 38
5
votes
1 answer

Separable filter on numpy array

Say I have a numpy array a, and I want to create a new array, b such that b[i, j] is a function of, say: a[i-1, j-1], a[i-1, j ], a[i-1, j+1], a[i , j-1], a[i , j ], a[i , j+1], a[i+1, j-1], a[i+1, j ], a[i+1, j+1] What would be the fastest…
Valentin Lorentz
  • 9,556
  • 6
  • 47
  • 69
5
votes
2 answers

Are there some cases where Python threads can safely manipulate shared state?

Some discussion in another question has encouraged me to to better understand cases where locking is required in multithreaded Python programs. Per this article on threading in Python, I have several solid, testable examples of pitfalls that can…
Erik Garrison
  • 1,697
  • 1
  • 14
  • 13
5
votes
1 answer

Python and truly concurrent threads

I've been reading for hours now and I can completely figure out how python multi threading is faster than a single thread. The question really stems from GIL. If there is GIL, and only one thread is really running at any single time, how can multi…
Anton
  • 2,282
  • 26
  • 43
5
votes
2 answers

Python cost of locking vs. performance, (does multithreading make sense?)

I'm working on a project where throughput of my code quite important and after some consideration I choose to make my program threaded. The main thread and the subthread both adds and removes from two shared dictionaries. I've been looking through…
Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66
4
votes
4 answers

Parallel file matching, Python

I am trying to improve on a script which scans files for malicious code. We have a list of regex patterns in a file, one pattern on each line. These regex are for grep as our current implementation is basically a bash script find\grep combo. The…
user974896
  • 1,795
  • 4
  • 28
  • 48
4
votes
1 answer

Will FastAPI application with only async endpoints encounter the GIL problem?

If all the fastapi endpoints are defined as async def, then there will only be 1 thread that is running right? (assuming a single uvicorn worker). Just wanted to confirm in such a setup, we will never hit the python's Global Interpreter Lock. If the…
4
votes
1 answer

why write async code in python while GIL exists?

I am wondering, if python GIL allow only a single thread / process to run at once, why should I use asyncio, I get that switching between threads is expensive but, thats it? this is the only advantage of asyncio in python?
dsal3389
  • 658
  • 1
  • 7
  • 26
4
votes
1 answer

How does ThreadPoolExecutor utilise 32 CPU cores for CPU bound tasks

From ThreadPoolExecutor Changed in version 3.8: Default value of max_workers is changed to min(32, os.cpu_count() + 4). This default value preserves at least 5 workers for I/O bound tasks. It utilizes at most 32 CPU cores for CPU bound tasks which…
Rufus
  • 5,111
  • 4
  • 28
  • 45
4
votes
2 answers

Does calling a c function via ctypes in python release the GIL during execution of the C code

I want to call some c function from python to be able to improve performance of my code. But I cannot find online whether when I call a C function using the ctypes libraries the GIL is released. As a simple example: from ctypes import * fun =…
user2882307
4
votes
1 answer

Why reversed() removes thread safety?

A common idiom in CPython to ensure thread safety for iteration is using tuple(). For example - tuple(dict.items()) is guaranteed to be threadsafe in CPython even if items are removed by a different thread. This is because the interpreter does not…
Bharel
  • 23,672
  • 5
  • 40
  • 80
4
votes
1 answer

Multithreading accelerates CPU bound tasks despite of GIL

I recently learned about GIL in python. I was doing some benchmarks and found out that multithreading actually does improve the performance. I compare elementwise NumPy operations that do not use any internal multithreading. In the first test, I…
Mikhail Genkin
  • 3,247
  • 4
  • 27
  • 47
4
votes
1 answer

400 threads in 20 processes outperform 400 threads in 4 processes while performing an I/O-bound task

Experimental Code Here is the experimental code that can launch a specified number of worker processes and then launch a specified number of worker threads within each process and perform the task of fetching URLs: import multiprocessing import…
Susam Pal
  • 32,765
  • 12
  • 81
  • 103