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

What is runtime in context of Python? What does it consist of?

In context to this question What is “runtime”? (https://stackoverflow.com/questions/3900549/what-is-runtime/3900561) I am trying to understand what would a python runtime be made of. My guess is: The python process that contains all runtime…
sprksh
  • 2,204
  • 2
  • 26
  • 43
7
votes
2 answers

Threading in Flask not working with UWSGI but working on commandline

I have a Flask application that works fine when run on the commandline, but when it is run through uWSGI it doesn't respond correctly to requests or the worker thread doesn't work properly. I've rewritten a simple proof-of-concept/failure program…
AndrewWhalan
  • 417
  • 3
  • 12
7
votes
4 answers

How can an interpretive language avoid using Global Interpreter lock (GIL)?

CPython uses a Global Interpreter Lock. Linux has removed all traces of the Big Kernel Lock. What is the alternative to these locks? How can a system make full use of a truly multi-core or multi-processor system without grinding everything to a…
rook
  • 66,304
  • 38
  • 162
  • 239
7
votes
1 answer

Is the GIL released while using multithreading with python-opencv?

I am doing heavy image processing in python3 on a large batch of images using numpy and opencv. I know python has this GIL which prevents two threads running concurrently. A quick search on Google told me that, do not use threads in python for CPU…
Abhijit Balaji
  • 1,870
  • 4
  • 17
  • 40
7
votes
1 answer

What are the implications of calling NumPy's C API functions from multiple threads?

This is risky business, and I understand the Global Interpreter Lock to be a formidable foe of parallelism. However, if I'm using NumPy's C API (specifically the PyArray_DATA macro on a NumPy array), are there potential consequences to invoking it…
ide
  • 19,942
  • 5
  • 64
  • 106
7
votes
2 answers

Does `psycopg2` in Python 2.x block the GIL

I'm trying to settle an argument with a coworker. Say that I have a Python 2.6 app that uses psycopg2 to communicate with a Postgres database. The app is multithreaded. When a thread makes a database call using psycopg2, does it release the GIL so…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
7
votes
2 answers

how is spacy-io using multi threading without GIL?

Referring this post Multi-Threaded NLP with Spacy pipe which talks about that, and here from https://spacy.io/ from spacy.attrs import * # All strings mapped to integers, for easy export to numpy np_array = doc.to_array([LOWER, POS, ENT_TYPE,…
user2290820
  • 2,709
  • 5
  • 34
  • 62
7
votes
1 answer

Calling python function from C as a callback. What is the right way to handle the GIL?

I'm using cytpes to wrap a C api. One of the api functions allows you to register a callback. I'm using CFUNCTYPE to to specify the function's type and make an instance of CFUNCTYPE from a python function that the user of my python library provides…
Stephen
  • 2,613
  • 1
  • 24
  • 42
7
votes
2 answers

Why does this Python script run 4x slower on multiple cores than on a single core

I'm trying to understand how CPython's GIL works and what are the differences between GIL in CPython 2.7.x and CPython 3.4.x. I'm using this code for benchmarking: from __future__ import print_function import argparse import resource import…
user108884
  • 71
  • 1
  • 5
7
votes
3 answers

Threading in Python using multiple cores

As far as I know, Python's threading library uses POSIX threads for threading and it does not run on multiple cores. So is it possible that we implement a multicore threading system for Python threads using Open MP?
xennygrimmato
  • 2,646
  • 7
  • 25
  • 47
7
votes
4 answers

multi-threading in python: is it really performance effiicient most of the time?

In my little understanding, it is the performance factor that drives programming for multi-threading in most cases but not all. (irrespective of Java or Python). I was reading this enlightening article on GIL in SO. The article summarizes that…
brain storm
  • 30,124
  • 69
  • 225
  • 393
7
votes
2 answers

python + wsgi on a multi-threaded web-server: is this a race condition?

Suppose that I've written a wsgi application. I run this application on Apache2 on Linux with multi-threaded mod-wsgi configuration, so that my application is run in many threads per single process: WSGIDaemonProcess mysite processes=3 threads=2…
Boris Burkov
  • 13,420
  • 17
  • 74
  • 109
7
votes
3 answers

How to avoid gcc warning in Python C extension when using Py_BEGIN_ALLOW_THREADS

The simplest way to manipulate the GIL in Python C extensions is to use the macros provided: my_awesome_C_function() { blah; Py_BEGIN_ALLOW_THREADS // do stuff that doesn't need the GIL if (should_i_call_back) { …
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
7
votes
3 answers

Releasing Python GIL while in C++ code

I've got a library written in C++ which I wrap using SWIG and use in python. Generally there is one class with few methods. The problem is that calling these methods may be time consuming - they may hang my application (GIL is not released when…
uhz
  • 2,468
  • 1
  • 20
  • 20
7
votes
3 answers

Could a C extension for multithreaded Python boost performance?

I have heard of Python's GIL problem, which states that there can only be one Python thread executing the Python bytecode at one time in a multicore machine. So a multithreaded Python program is not a good idea. I am wondering if I can write a C…
ming.kernel
  • 3,365
  • 3
  • 21
  • 32