r/Python • u/ThatsAHumanPerson • Apr 18 '24
Resource Achieve true parallelism in Python 3.12
Article link: https://rishiraj.me/articles/2024-04/python_subinterpreter_parallelism
I have written an article, which should be helpful to folks at all experience levels, covering various multi-tasking paradigms in computers, and how they apply in CPython, with its unique limitations like the Global Interpreter Lock. Using this knowledge, we look at traditional ways to achieve "true parallelism" (i.e. multiple tasks running at the same time) in Python.
Finally, we build a solution utilizing newer concepts in Python 3.12 to run any arbitrary pure Python code in parallel across multiple threads. All the code used to achieve this, along with the benchmarking code are available in the repository linked in the blog-post.
This is my first time writing a technical post in Python. Any feedback would be really appreciated! ๐
12
u/Mount_Gamer Apr 18 '24
Any idea if the sub interpreters will use less memory than the multiprocessing method?
12
u/120decibel Apr 18 '24
You might want to cover multiprocessing a bit more in dept as it site-steps the GIL through subprocesses as well. Otherwise great write up, I enjoyed reading it a lot! Thanks!
3
u/ThatsAHumanPerson Apr 18 '24
Really appreciate all the positive response here!
I've made a few changes incorporating the common feedback points:-
2
u/[deleted] Apr 19 '24
Great write up though I'd really recommend limiting the text width of your text content. Currently it's very tought to read 200+ character lines on desktop. You can do that with css style
max-width: 70ch(for 70character limit)43
u/ryanstephendavis Apr 18 '24
Well-written article... good refresher on a lot of software engineering concepts and it's cool to see some code with the new subinterpreters!
11
u/andrejlr Apr 18 '24
Nice summary. Was looking forward to subibinterpreters. This article shows though that they have and probably for a long time will have too many limitations.
Practically all production code in my experience is using some native extensions . And after reading this I just don't want to start new experiments for parallel code which is already working .
Often, starting new processes is not such a big penalty, as each of them might run hours of computation and consume magnitudes more memory than the pure Python process itself . So one can easily afford it .
The real overhead within the default process.Pool lib is the need to serialize ( pickle ) objects between the main process and worker processes .
In our experienc, this is where Rey is unbeatable for single machine parallelism . It will give you even same process.Pool.map interface as a one-line replacement, if you want . But under the hood, it is using pyarrow memory format and also a pyarrow store allowing multiple Proccesses to do a direct memory access without serialization
4
u/[deleted] Apr 18 '24
Nice to see. In Py 3.13 this should be implementable in pure python, hopefully
7
u/protienbudspromax Apr 18 '24
Nice one OP. I knew about that GIL pep but wasnt sure what they implemented in place of that. Was a good read!
1
u/Jesus-was-a-mushroom Apr 19 '24
Link broken?
3
u/JayZFeelsBad4Me Apr 18 '24
Side note - your website looks great but any plans to add dark mode?
3
u/WhiteGoldRing Apr 18 '24
Why not use the multiprocessing nodule?
4
u/bajadrix Apr 18 '24
You can obtain true parallelism in pure Python with multiprocess approach by using multiprocessing module.
1
u/barath_chinmala Apr 19 '24
Truly informative and a well written article man. Keep up the good work
-46
u/worriedjacket Apr 18 '24
Insane to use C++ instead of rust
-2
u/Glass_Dingo_3984 Apr 18 '24
-5
u/Glass_Dingo_3984 Apr 18 '24
Ow yes if you us turtle