Python's Multi-Threading and the GIL
Multi-threading in Python can get complicated. Read on to see what one senior developer has to say resolving some of the complexity.
Join the DZone community and get the full member experience.Join For Free
Got this in an email.
"Python's multi-threading module seems not efficient because of the global interpreter lock?"
Is the trick is to use " Thread-Local Data"?
It Gets Worse
Interestingly, there was no further ask. The questioner had decided on thread-local data because the questioner had decided to focus on threads. And they were done making choices at that point.
No question on "What was recommended?" or "What's a common solution?" or "What is Dask?" Nothing other than "confirm my assumptions."
This is swirling around a bunch of emails on trying to determine the maximum number of concurrent threads or processes based on the number of cores or CPUs or something.
I'll repeat that for those who skim.
They think there's a maximum number of concurrent threads or processes.
If you have some computation which (1) makes zero OS requests and (2) is never interrupted, I can imagine you'd like to have all of the cores fully committed to executing that theoretical stream of instructions. You might even be able to split that theoretical workload up based on the number of cores.
Practically, however, that stream of uninterrupted computing rarely exists.
Maybe. Maybe you've got some basin-hopping or gradient-following or random forest ML algorithm which is going to do a burst of computation on an in-memory data structure. In that (rare) case, Dask is still ideal for exploiting all of the cores on your processor.
The upper-bound idea bugs me a lot.
- Any OS request leads to a context switch. Any context switch leads to waiting. Any waiting means you can have more threads.
- As far as I know, any memory write outside the local cache will lead to a stall in the pipeline. Another thread can (and should) leap into the core's processing stream. The only way you can create the "all-computing" sequence of instructions bounded by the number of cores is to *also* be sure the entire thing fits in cache.
What's the maximum number of threads or processes? It depends on the wait times. It depends on memory writes. It depends on the size of the data structure, the size of the cache, and the size of the instruction stream.
Because it depends on a lot of things, it's rather difficult to predict. And that makes it rather difficult to determine a maximum.
Replying about the uselessness of trying to establish a maximum, of course, does nothing. As far as I know, they're still assiduously trying to use
os.sched_getaffinity() to put an upper bound on the size of a thread pool.
Acting as if Dask doesn't exist.
Use Dask or use a multiprocessing pool.
These are simple things. They don't require a lot of hand-wringing over the GIL and Thread Local Data. They're built. They work. They're simple and effective solutions.
Published at DZone with permission of Steven Lott, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.