News for hackers. Stuff that matters.
Re:The GIL and its effects on Python multithreading by r4victor on Wednesday September 29, 2021 @06:21AM (#28691988)
Hi! I'm the author of this article. Thanks for posting it.

The GIL is an old topic, but I was surprised to learn recently that it's much more nuanced than I thought. So, here is my little research.

This article is a part of my series that dives deep into various aspects of the Python language and the CPython interpreter. The topics include: the VM; the compiler; the implementation of built-in types; the import system; async/await. Check out the series if you liked this post: https://tenthousandmeters.com/tag/python-behind-the-scenes

I welcome your feedback and questions, thanks!

Re:The GIL and its effects on Python multithreading by sirfz on Wednesday September 29, 2021 @06:46AM (#28692113)
Haven't checked the full series yet but this GIL article is very nicely written and insightful. Thank you
Re:The GIL and its effects on Python multithreading by gadrev on Wednesday September 29, 2021 @07:47AM (#28692465)
Wow, just skimmed over a few articles in the series and they seem very nice and well researched. Congratulations.
Re:The GIL and its effects on Python multithreading by brodouevencode on Wednesday September 29, 2021 @02:07PM (#28697579)
You're doing great work, and this isn't the first article from your site that I've passed along to my coworkers. Keep it up!
Re:The GIL and its effects on Python multithreading by jeroenvlek on Wednesday September 29, 2021 @07:21AM (#28692302)
You filled in a lot of gaps in my GIL knowledge. Today I learned something new. Thank you for writing it!
Re:The GIL and its effects on Python multithreading by harpiaharpyja on Wednesday September 29, 2021 @09:22AM (#28693217)
In the opening paragraph you state that the GIL prevents speeding up CPU-intensive code by distributing the work among multiple threads.

My understanding is that distributing work across multiple threads would not speed up CPU-intensive code anyways. In fact it would add overhead due to threading.

Perhaps you meant I/O bound code?

Re:The GIL and its effects on Python multithreading by aardvark179 on Wednesday September 29, 2021 @09:35AM (#28693403)
There are two things you should consider here, wall clock time, and cpu time. Making code faster using multiple threads will increase CPU time by some amount, but because that work is now distributed between several cores it should actually reduce wall clock time.

There are many CPU bound tasks which can be made multithreaded and faster, but it does depend on the task and how much extra coordination you’re adding to make it multithreaded.

Re:The GIL and its effects on Python multithreading by Jtsummers on Wednesday September 29, 2021 @11:20AM (#28694892)
The author is speaking about the general concept of threading, outside of Python (without using C extensions to help out as discussed in the article). In general, if you don't have a GIL, and you have 2 or more cores then if you run additional threads you will see a speedup for CPU-intensive code. The actual speedup will vary. A sibling comment mentions embarrassingly parallel problems, those are things like ray tracing, where each computation is independent of all the others. In those cases, you get near linear speedup with each additional core and thread. If there is more coordination between the threads (mutexes and semaphores, for instance, controlling access to a shared datum or resource) then you will get a less-than-linear speedup. And if there is too much contention for a common resource, you won't get any speedup and will see some slowdown due to the overhead introduced by threads.
Re:The GIL and its effects on Python multithreading by harpiaharpyja on Wednesday September 29, 2021 @01:17PM (#28696806)
Oh, I thought you needed multiple processes for this.
Re:The GIL and its effects on Python multithreading by jjice on Wednesday September 29, 2021 @09:30AM (#28693302)
If it was being distributed amongst python threads (which run on one hardware thread), then CPU performance can't improve since they're just taking turns using the CPU. If you're running on multiple hardware threads (what I assume the author meant), that can causes better CPU performance since it will distribute work across real threads that can run in parallel.

The GIL restricts use of multiple hardware threads.

Re:The GIL and its effects on Python multithreading by pwiecz on Wednesday September 29, 2021 @09:30AM (#28693318)
You can very well parallelise CPU-intensive problems. Look at e.g. "Embarassingly parallel" on Wikipedia. Intuitively if you can divide your work into chunks that are large enough, the scheduling overhead becomes negligible.
Re:The GIL and its effects on Python multithreading by dekhn on Wednesday September 29, 2021 @09:50AM (#28693640)
what are you talking about? worker thread pools are the most common way to take advantage multiple cores. Typically can see speedups (for highly parallel codes) of nX for n cores.
Re:The GIL and its effects on Python multithreading by tempest_ on Wednesday September 29, 2021 @10:36AM (#28694207)
In python there is nuance to the term threads.

Threads in python will run in the same python process and use only one core.

If you want to use more cores you need to use more python processes.

Re:The GIL and its effects on Python multithreading by pcl on Wednesday September 29, 2021 @11:23AM (#28694930)
... but that's because of these very GIL limitations, is it not?
Re:The GIL and its effects on Python multithreading by tempest_ on Wednesday September 29, 2021 @12:00PM (#28695510)
Sure, but I think if you are discussing this type of thing in the context of python you have to use the threads/processes terminology to avoid confusion.
Re:The GIL and its effects on Python multithreading by simiones on Wednesday September 29, 2021 @01:02PM (#28696569)
Not in an article explaining why those limitations exist at all.
Re:The GIL and its effects on Python multithreading by cameldrv on Wednesday September 29, 2021 @01:49PM (#28697300)
The reason why this is true in Python is the GIL. In other languages without a GIL, multiple threads will run on multiple cores and can speed up CPU bound code.