Python is NOT Single Threaded (and how to bypass the GIL)
Why Stack Overflow recommends it
Stack Overflow users link it when explaining the GIL and threading limits, and to show that compiled extensions can run in parallel without resorting to multiprocessing.
Recommended by 1 people across 1 different months, 2020 to 2023.
What it is
A short YouTube explainer by Jack of Some arguing that Python is not strictly single threaded. It shows that threads can use multiple CPU cores when the heavy work is handed to a C extension such as numba that releases the GIL, rather than relying only on multiprocessing.
What people said
Recommends Michał Darowny · 2021-05 · Stack Overflow · on “Python: How do we parallelize a python program to take advantage of a GPU server?”
Do not go into multiprocessing in python if you want to make use of many cores at once. [this video] check out this video which explains in more detail cons of multiprocessing.
Cites Andres Bonilla · 2020-12 · Stack Overflow · on “Threading: Second thread doesn't run in parallel”
When creating your threads there shouldn't be parentheses on the parameter to target. You are calling the function and then (which in this case is never) passing the result of that function as an argument, instead you should pass the function to be called (only the name, without parentheses). To answer the question on the title, the Python interpreter only allows execution of one single thread at a time due to the Global Interpreter Lock, as far as I know it's used primarily for memory safety but it won't allow true parallelism from pure Python. You can still do parallel tasks using multiproccess or a compiled C function (at least on CPython) since those can bypass the GIL, but you'll have t
Cites gman · 2021-06 · Stack Overflow · on “It is said that python doesn't support multithreading, then why does it have a threading module?”
Python's single-threaded nature is due to the GIL (Global interpreter lock). When people refer to python being single threaded, they are describing how python operates when not using the threading or multiprocessing libraries. You can still make python use more threads, or spin up multiple processes, but for each instance of the code that you are running, it will only be using a single thread. Javascript for example can make use of multiple threads and doesn't require any additional "work" to make this happen. Check out this video for some more info: [this video]
Cites mike rodent · 2023-07 · Stack Overflow · on “Latency in detecting key press (hotkey) due to non-event-thread activity”
I think I finally detected the problem ... it's classic and it may be of interest to someone baffled by a similar situation. In my worker thread (non-event thread) I had multiple time.sleep(.0001) commands, the purpose of which is to allow other threads (including the event thread aka "main thread") to wrest control ... as previously explained here [LINK] by Musicamante in reply to an earlier question by me. The purpose of my worker thread, as mentioned, is to split apart Word (.docx) documents for analysis, using docx2python [LINK] , and the analysis itself is "CPU-intensive". I was slightly surprised to find that, even for a very large file, this command took very little time: docx_obj = d