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
9
votes
2 answers

Why Python is not better in multiprocessing or multithreading applications than Java?

Since Python has some issues with GIL, Java is better for developing multiprocessing applications. Could you please justify the exact reasoning of java's effective processing than python in your way?
Nava
  • 6,276
  • 6
  • 44
  • 68
9
votes
2 answers

Why does time.sleep(...) not get affected by the GIL?

From what I understood when doing research on the Python GIL, is that only one thread can be executed at the once (Whoever holds the lock). However, if that is true, then why would this code only take 3 seconds to execute, rather than 15…
user12388651
  • 179
  • 1
  • 8
9
votes
3 answers

Regarding GIL in python

I know GIL blocks python from running its threads across cores. If it does so, why python is being used in webservers, how are the companies like youtube, instagram handling it. PS: I know alternatives like multiprocessing can solve it. But it would…
9
votes
1 answer

How to release the GIL in Cython for a multithreaded C++ class?

I have a C++ class with some methods that use std::thread that I'm making accessible to Python via Cython. Do you know where in my Cython code I'd want to put the nogill directive? Would I want to put it when I declare the class methods or when I…
Jack Simpson
  • 1,681
  • 3
  • 30
  • 54
9
votes
1 answer

Python UDP socket send bottleneck (slow/delays randomly)

Python UDP Streamer with hickup in sending I'm currently developing on a python 3.4 network streaming app. And i have some crazy behavior with my socket. (Target 3.3 compatible if possible) Definition: When i talk of Stream an UDP-Stream is meant.…
Jan
  • 91
  • 1
  • 5
8
votes
1 answer

Do I need to use `nogil` in Cython

I have some Cython code that I'd like to run as quickly as possible. Do I need to release the GIL in order to do this? Let's suppose my code is similar to this: import numpy as np # trivial definition just for illustration! cdef double…
DavidW
  • 29,336
  • 6
  • 55
  • 86
8
votes
2 answers

Is it true that in multiprocessing, each process gets it's own GIL in CPython? How different is that from creating new runtimes?

Are there any caveats to it? I have a few questions related to it. How costly is it to create more GILs? Is it any different from creating a separate python runtime? Once a new GIL is created, will it create everything (objects, variables, stack,…
sprksh
  • 2,204
  • 2
  • 26
  • 43
8
votes
2 answers

GIL behavior in python 3.7 multithreading

I am researching and trying to understand the python GIL and best practices to use multithreading in python. I found this presentation and this video I tried to reproduce the strange and crazy problems mentioned in the first 4 slides of the…
8
votes
1 answer

Cython's prange not improving performance

I'm trying to improve the performance of some metric computations with Cython's prange. Here are my codes: def shausdorff(float64_t[:,::1] XA not None, float64_t[:,:,::1] XB not None): cdef: Py_ssize_t i Py_ssize_t n =…
mavillan
  • 365
  • 1
  • 2
  • 13
8
votes
1 answer

How do I determine the appropriate check interval?

I'm just starting to work on a tornado application that is having some CPU issues. The CPU time will monotonically grow as time goes by, maxing out the CPU at 100%. The system is currently designed to not block the main thread. If it needs to do…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
7
votes
1 answer

SWIG C++ Python polymorphism and multi-threading

I'm integrating a 3rd party C++ package to a python application using SWIG. The package connects to a proprietary API over a network and receives updates. The overall flow is that python instantiates a C++ object, calls its functions to set it up…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
7
votes
0 answers

Is using FastAPI with sync threads a good practice for a high scale application?

I'm using FastAPI with non-async endpoints running over gunicorn with multiple workers, from the uvicorn.workers.UvicornWorker class as suggested here. Latley, I noticed high latency in some of our endpoints during times in the day our application…
7
votes
0 answers

Isolated Sub-Interpreters in Python without GIL

There are PEP-554 and PEP-684. Both are designed to support multiple interpreters on Thread level. Does anyone know if these PEPs are implemented somewhere at least in experimental or pre-release versions of Python like 3.11? I found out that Python…
Arty
  • 14,883
  • 6
  • 36
  • 69
7
votes
2 answers

Why does this Python code with threading have race conditions?

This code creates a race condition: import threading ITERS = 100000 x = [0] def worker(): for _ in range(ITERS): x[0] += 1 # this line creates a race condition # because it takes a value, increments and then writes #…
culebrón
  • 34,265
  • 20
  • 72
  • 110
7
votes
2 answers

How asyncio.sleep isn't blocking thread?

I'm reading 'Fluent Python' by 'Luciano Ramalho' over and over, but I couldn't understand asyncio.sleep's behavior inside asyncio. Book says at one part: Never use time.sleep in asyncio coroutines unless you want to block the main thread, therefore…
jupiterbjy
  • 2,882
  • 1
  • 10
  • 28
1 2
3
25 26