7 ms·
One thing I'm curious about here is the operational impact. In production systems we often see Python services scaling horizontally because of the GIL limitati
by devrimozcay 6mo ago
One thing I'm curious about here is the operational impact.
In production systems we often see Python services scaling horizontally
because of the GIL limitations. If true parallelism becomes common,
it might actually reduce the number of containers/services needed
for some workloads.
But that also changes failure patterns — concurrency bugs,
race conditions, and deadlocks might become more common in
systems that were previously "protected" by the GIL.
It will be interesting to see whether observability and
incident tooling evolves alongside this shift.
- matsemann 6mo agoFor big things the current way works fine. Having a separate container/deployment for celery, the web server, etc is nice so you can deploy and scale separately. Mostly it works fine, but there are of course some drawbacks. Like prometheus scraping of things then not able to run a web server in parallel etc is clunky to work around. And for smaller projects it's such an annoyance. Having a simple project running, and having to muck around to get cron jobs, background/async tasks etc. to work in a nice way is one of the reasons I never reach for python in these instances. I hope removing the GIL makes it better, but also afraid it will expose a whole can of worms where lots of apps, tools and frameworks aren't written with this possibility in mind.
- apothegm 6mo agoA lot of that has already been solved for by scaling workers to cores along with techniques like greenlets/eventlets that support concurrency without true multithreading to take better advantage of CPU capacity.
- kevincox 6mo agoBut you are still more or less limited to one CPU core per Python process. Yes, you can use that core more effectively, but you still can't scale up very effectively.
- Sohcahtoa82 6mo agoThat's great for concurrency, but doesn't improve parallelism. Unless you mean you have multiple worker processes (or GIL-free threads).
- apothegm 6mo agoYes, multiple worker processes is what I meant. Few web apps have a meaningful use for parallelism within a single process. So long as you’re keeping all cores busy with independent processes at high concurrency, multithreading adds relatively little. YMMV if you’re doing a lot of number crunching.
- kevincox 6mo agoThis is surely why Facebook was interested in funding this work. It is common to have N workers or containers of Python because you are generally restricted to one CPU core per Python process (you can get a bit higher if you use libs that unlock the GIL for significant work). So the only scaling option is horizontal because vertical scaling is very limited. The main downside of this was memory usage. You would have to load all of your code and libraries N types and in-process caches would become less effective. So by being able to vertically scale a Python process much further you can run less and save a lot of memory. Generally speaking the optimal horizontal scaling is as little as you have to. You may want a bit of horizontal scaling for redundancy and geo distribution, but past that vertically scaling to fewer larger process tend to be more efficient, easier to load balance and a handful of other benefits.
- philsnow 6mo ago> The main downside of this was memory usage. You would have to load all of your code and libraries N types and in-process caches would become less effective. You can load modules and then fork child processes. Children will share memory with each other (if they need to modify any shared memory, they get copy-on-write pages allocated by the kernel) and you'll save quite a lot on memory.
- kevincox 6mo agoYes, this can help a lot, but it definitely isn't perfect. Especially since CPython uses reference counting it is likely that many pages get modified relatively quickly as they are accessed. Many other GC strategies are also pretty hostile to CoW memory (for example mark bits, moving, ...) Additionally this doesn't help for lazy loaded data and caches in code and libraries.
- cma 6mo agoEvery python object will trigger copy on write of a full memory page on any read, due to reference counting, though some will share pages.
- LtWorf 6mo agoBut python can fork itself and run multiple processes into one single container. Why would there be a need to run several containers to run several processes? There's even the multiprocessing module in the stdlib to achieve this.
- kccqzy 6mo agoForking and multi threading do not coexist. Even if one of your transitive dependencies decides to launch a thread that’s 99% idle, it becomes unsafe to fork.
- philsnow 6mo agoFork-then-thread works, does it not?
- rpcope1 6mo agoBut not the reverse, if its a bare fork and not strictly using basically mutex and shared resource free code (which is hard), and there's little or no warning lights to indicate that this is a terrible idea that fails in really unpredictable and hard to debug ways.
- kccqzy 6mo agoIf you have enough discipline to make sure you only create threads after all the forking is done, then sure. But having such discipline is harder than just forbidding fork or forbidding threads in your program. It turns a careful analysis of timing and causality into just banning a few functions.
- josefx 6mo agoCan't you check what threads are active at the time you fork?
- kccqzy 6mo agoAnd what do you do with that information? Refuse to fork after you detect more than one thread running? I haven’t seen any code that gracefully handles the unable-to-fork scenario. When people write fork-based code, especially in Python, they always expect forking to succeed.
- rpcope1 6mo ago> observability tooling for Python evolving As much as I dislike Java the language, this is somewhere where the difference between CPython and JVM languages (and probably BEAM too) is hugely stark. Want to know if garbage collection or memory allocation is a problem in your long running Python program? I hope you're ready to be disappointed and need to roll a lot of stuff yourself. On the JVM the tooling for all kinds of observability is immensely better. I'm not hopeful that the gap is really going to close.
- mike_hearn 6mo agoYou can run Python on the JVM and then benefit from those tools!
- fiedzia 6mo ago> If true parallelism becomes common, it might actually reduce the number of containers/services needed for some workloads Not by much. The cases where you can replace processes with threads and save memory are rather limited.
- aoeusnth1 6mo agoCitation needed? Tall tasks are standard practice to improve utilization and reduce hotspots by reducing load variance across tasks.
- influx 6mo agoI would have thought most of those would have been moved to async Python by now.
- LtWorf 6mo agoasync python still uses a single thread for the main loop, it just hides non blocking IO.