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?
Asked
Active
Viewed 6,591 times
2 Answers
33
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.

Alex Martelli
- 854,459
- 170
- 1,222
- 1,395
-
5A "Plain Old Unix Pipeline" of Python applications is often a better design than multiple threads. A pipeline of processes avoids all GIL issues. It also avoids any potential thread-safety issues in any library you happen to be using. It's easy to create using the shell. The OS handles synchronization for you -- you just read from sys.stdin and write to sys.stdout. – S.Lott Jun 14 '09 at 09:56
-
@S.Lott does that involve running scripts from `subprocess` if calling from a python app? – Charlie G Oct 26 '22 at 13:16
2
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.

Merijn
- 329
- 2
- 4
-
2This is a common misconception about stackless. Stackless does not help take advantage of multiple cores. See http://www.stackless.com/pipermail/stackless/2007-August/001963.html – Jay Taylor Apr 06 '11 at 16:56