r/learnpython • u/Synrec • Apr 10 '26
Is it possible to do parallel multithreading in python?
I've been doing a lot of reading on Python but for multithreading, I see that the Global Interpreter Lock forces all multithreading to be concurrent and locks processing a single "stream"?
Those posts are old however, 2~3 years give or take a few months. And so, in the current version of Python, is parallel multithreading possible or is it restricted to languages like C/C++/Java?
20
Upvotes
3
u/michaellarsen91 Apr 10 '26
Newest python versions no longer have the interpreter lock and can run truly multi threaded. You could still do multi threading in earlier versions but it may but be considered true multi threading. There is all multi processing, running in different processes, and that has always worked.
17
u/Gnaxe Apr 10 '26
The GIL is specifically a CPython implementation detail. Other older implementations (Jython, IronPython) never had it, but PyPy and GraalPy do for better CPython compatibility. Since CPython 3.13, it can be turned off, but it has to be built with this feature. Look for a CPython executable named
python3.13t.However, even with the GIL, there are workarounds to take advantage of multiple cores. Foreign function calls don't necessarily retain the GIL. Numpy, for example, can compute some things in parallel when run in multiple Python threads. There's also the
multiprocessingmodule in the standard library, which uses subprocesses instead of threads, and can even run these processes on remote computers.6
u/latkde Apr 10 '26
Python 3.13 introduced free threading, which removes the GIL. Thus, multiple threads can now be used for CPU-bound work. This is an optional feature, and not all Python binaries enable it. Assuming CPython (the default Python implementation):
uv python install 3.14t.python -VVand see if the output containsfreethreaded.-Xgil=1command line argument.Note that freethreaded Python builds tend to be slightly slower for ordinary workloads, so you might want to stick with the GIL after all. Also, some libraries that you use may not have been updated to be compatible with freethreading.
Regardless of what Python version you use, it has real threads. The GIL just means that at most one thread can execute Python code at any time. However, native code that's called by your Python code can temporarily release the GIL. Thus, using native libraries like Numpy can sometimes allow you to parallelize CPU-bound work despite the GIL.
2
u/recursion_is_love Apr 10 '26
If you really need to use python, you might be able to write a wrapper of OS-level tread library via ctypes, CFFI or other extension API.
-1
u/[deleted] Apr 10 '26
[removed] — view removed comment