Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does running separate python processes avoid the GIL?

Tags:

I'm curious in how the Global Interpreter Lock in python actually works. If I have a c++ application launch four separate instances of a python script will they run in parallel on separate cores, or does the GIL go even deeper then just the single process that was launched and control all python process's regardless of the process that spawned it?

like image 367
whatWhat Avatar asked Jun 14 '09 04:06

whatWhat


People also ask

Does subprocess release the GIL?

When calling a linux binary which takes a relatively long time through Python's subprocess module, does this release the GIL? Yes, it releases the Global Interpreter Lock (GIL) in the calling process.

How multithreading works in Python with GIL?

The GIL allows only one OS thread to execute Python bytecode at any given time, and the consequence of this is that it's not possible to speed up CPU-intensive Python code by distributing the work among multiple threads. This is, however, not the only negative effect of the GIL.

What is an alternative to GIL in Python?

Alternative Python interpreters: Python has multiple interpreter implementations. CPython, Jython, IronPython and PyPy, written in C, Java, C# and Python respectively, are the most popular ones. GIL exists only in the original Python implementation that is CPython.


2 Answers

The GIL only affects threads within a single process. The multiprocessing module is in fact an alternative to threading that lets Python programs use multiple cores &c. Your scenario will easily allow use of multiple cores, too.

like image 134
Alex Martelli Avatar answered Sep 22 '22 18:09

Alex Martelli


As Alex Martelli points out you can indeed avoid the GIL by running multiple processes, I just want to add and point out that the GIL is a limitation of the implementation (CPython) and not of Python in general, it's possible to implement Python without this limitation. Stackless Python comes to mind.

like image 30
Merijn Avatar answered Sep 20 '22 18:09

Merijn