News for hackers. Stuff that matters.
Re:Python stands to lose its GIL, and gain a lot of speed by TekMol on Sunday October 17, 2021 @12:07PM (#28897444)
Why is multithreaded performance important, what are the usecases where you cannot run multiple processes to spread your numbercrunching across CPUs?

I am praying for CPython to become faster. But I need faster singlethreaded performance, so web applications benefit from it.

Re:Python stands to lose its GIL, and gain a lot of speed by lostdog on Sunday October 17, 2021 @01:54PM (#28898295)
Here's a use case: I was training a neutral net, and wanted to do some preprocessing (similar to image resizing, but without an existing C function). Inputs are batched, so the preprocessing is trivially parallelizable. I tried to multithread it in python, and got no speedup at all.

That was a really sad moment, and I've never felt good about python since.

Re:Python stands to lose its GIL, and gain a lot of speed by isoprophlex on Sunday October 17, 2021 @02:19PM (#28898499)
Yeah, I had something similar.

I wanted "as you wait for the GPU to churn through this batch, start reading the next batch from disk & preprocessing it on the CPU"

Getting this to work turned out so ass backwards it made me sad

Also I pity the fool who tries to connect a debugger to code using multiprocessing.Pool()...

Re:Python stands to lose its GIL, and gain a lot of speed by redeyed on Saturday October 23, 2021 @07:31AM (#28967451)
> Also I pity the fool who tries to connect a debugger to code using multiprocessing.Pool()

PyCharm works fine

Re:Python stands to lose its GIL, and gain a lot of speed by redeyed on Saturday October 23, 2021 @07:33AM (#28967461)
Yeah, had the exact problem.

I solved it by using python threads and C function (cv2.resize)

Re:Python stands to lose its GIL, and gain a lot of speed by chrisseaton on Sunday October 17, 2021 @12:08PM (#28897451)
Communicating between processes is more expensive than communicating between threads.
Re:Python stands to lose its GIL, and gain a lot of speed by TekMol on Sunday October 17, 2021 @12:16PM (#28897503)
Ok, but what is the use case where you need high bandwidth inter-thread/process communication?
Re:Python stands to lose its GIL, and gain a lot of speed by moron4hire on Sunday October 17, 2021 @12:27PM (#28897597)
Games. Simulations. Backend servers for shared editor experiences. Teleconferencing.
Re:Python stands to lose its GIL, and gain a lot of speed by chrisseaton on Sunday October 17, 2021 @12:30PM (#28897626)
Except for embarrassingly parallel problems, the trade-off of generating more parallelism is usually needing finer-grained communication. Canonical examples in the literature are matrix multiplication, triangulation, and refinement.
Re:Python stands to lose its GIL, and gain a lot of speed by Spivak on Sunday October 17, 2021 @01:10PM (#28897942)
Large shared state is basically always the answer. You can cop-out and say use a database or Redis if that’s fast enough but that’s just making someone else use many threads with shared memory.
Re:Python stands to lose its GIL, and gain a lot of speed by adgjlsfhk1 on Sunday October 17, 2021 @12:44PM (#28897732)
matrix multiply is an obvious one. partial differential equations are another. Sorting is one if you don't care about math.
Re:Python stands to lose its GIL, and gain a lot of speed by rbjorklin on Sunday October 17, 2021 @12:18PM (#28897528)
One thing that comes to mind is for shared connection pools when you have thousands of workers connecting to the same stateful service.
Re:Python stands to lose its GIL, and gain a lot of speed by TekMol on Sunday October 17, 2021 @12:38PM (#28897682)
Which use case requires such a setup?
Re:Python stands to lose its GIL, and gain a lot of speed by Spivak on Sunday October 17, 2021 @01:04PM (#28897889)
MySQL comes to mind. Unlike Postgres where connections are expensive MySQL encourages loading up the server with hundreds of simultaneous connections per server.

Like anything, it’s possible to split this work out to a separate process but the IPC overhead is a lot.

Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @12:42PM (#28897717)
Most databases (unfortunately), and since a ton of web apps use databases for state..

That said, many such databases are (or already have) rolled out connection pool proxies for reasons like this, so meh.

Re:Python stands to lose its GIL, and gain a lot of speed by lanstin on Sunday October 17, 2021 @12:47PM (#28897756)
Web front end backed by data base with expensive login, so you cache connections. Two tier architecture it would be called.
Re:Python stands to lose its GIL, and gain a lot of speed by VWWHFSfQ on Sunday October 17, 2021 @12:09PM (#28897455)
multiple processes use a lot more memory than threads
Re:Python stands to lose its GIL, and gain a lot of speed by TekMol on Sunday October 17, 2021 @12:10PM (#28897466)
Can you quantify that?
Re:Python stands to lose its GIL, and gain a lot of speed by redeyed on Saturday October 23, 2021 @07:38AM (#28967494)
There is such problem on Windows. When you create new process (spawn) it copies all memory of parent process.
Re:Python stands to lose its GIL, and gain a lot of speed by SkittyDog on Sunday October 17, 2021 @12:15PM (#28897494)
Yes, you can
Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @12:20PM (#28897540)
Even with copy-on-write? Have any actual numbers?
Re:Python stands to lose its GIL, and gain a lot of speed by kaba0 on Sunday October 17, 2021 @02:45PM (#28898720)
What about context switches as well, much slower IPC, and basically “no native support”.
Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @09:17PM (#28901328)
Depends on the workload if it matters or not. Some it matters a ton, some not a bit. And if you have n=cores processes, you can get some benefits with collocation and the like the OS can do for you.
Re:Python stands to lose its GIL, and gain a lot of speed by kaba0 on Monday October 18, 2021 @10:45AM (#28905954)
But I also meant to highlight that you can’t simply lock on an object, can’t yield/notify other threads, etc.

While performance may not matter, productivity very much suffers from multi-process programming.

Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Monday October 18, 2021 @02:53PM (#28909805)
I’m not saying performance is not different between these types of solutions or the dev work is not sometimes different.

I’m saying that the penalties (including development effort) for work co-ordinated between processes compared to threads can vary from nearly zero (sometimes even net better) to terrible depending on the nature of the workload. Threads are very convenient programmatically, but also have some serious drawbacks in certain scenarios.

For instance fork()’d sub-processes do a good job of avoiding segfaulting /crashing/dead locking everything all at once if there is a memory issue or other problem when doing the work, it’s very difficult in native threading models to do per-work resource management or quotas (like maximum memory footprint, or max number of open sockets, or max number of open files) since everything is grouped together at the OS level and it’s a lot of work to reinvent that yourself (which you’d need to do with threads). Also, the same shared memory convenience can cause some pretty crazy memory corruption in otherwise isolated areas of your system if you’re not careful, which is not possible with separate processes.

I do wish the python concurrency model was better. Even with it’s warts, it is still possible to do a LOT with it in a performant way. Some workloads are definitely not worth the trouble now however.

Re:Python stands to lose its GIL, and gain a lot of speed by SkittyDog on Sunday October 17, 2021 @12:27PM (#28897593)
Actual work is left as an exercise for the reader ;-)
Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @12:46PM (#28897746)
Last I did this, when the processes were fork()‘s of the parent (the typical way this was done), memory overhead was minimal compared to threads. A couple %. That was somewhat workload dependent however, if there is a lot of memory churn or data marshaling/unmarshalling happening as part of the workload, they’ll quickly diverge and you’ll burn a ton of CPU doing so.

Typical ways around that include mmap’ng things or various types of shared memory IPC, but that is a lot of work.

Re:Python stands to lose its GIL, and gain a lot of speed by adgjlsfhk1 on Sunday October 17, 2021 @12:18PM (#28897522)
each python process requires somewhere around 200mb of memory and .1s to do nothing. if you want libraries, it scales from there.
Re:Python stands to lose its GIL, and gain a lot of speed by TekMol on Sunday October 17, 2021 @12:33PM (#28897643)
Really? That sounds like an aweful lot.

When I execute this:

    python3 -c 'import time; time.sleep(60)'
And then pmap the process id of that process, I get 26144K. That is 26MB.

As for timing, when I execute this:

   time python3 -c ''
I get 0.02s
Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @12:48PM (#28897760)
Also generally no one spins up distinct, new processes for the ‘co-ordinated distinct process work queue’ when they can just fork(), which should be way faster and pretty much every platform uses copy-on-write for this, so also has minimal memory overhead (at least initially)
Re:Python stands to lose its GIL, and gain a lot of speed by jesboat on Sunday October 17, 2021 @01:19PM (#28898004)
The problem is (perhaps amusingly) with refcounting. As the processes run, they'll each be doing refcount operations on the same module/class/function/etc objects which causes the memory to be unshared.
Re:Python stands to lose its GIL, and gain a lot of speed by lazide on Sunday October 17, 2021 @02:02PM (#28898367)
Only where there is memory churn. If you’re in a tight processing loop (checksumming a file? Reading data in and computing something from it?) then the majority of objects are never referenced or dereferenced from the baseline.

Also, since the copy on write generally is memory page by memory page, even if you were doing a lot of that, if most of those ref counts are in a small number of pages, it’s not likely to really change much.

It would be good to get real numbers here of course. I couldn’t find anyone obviously complaining about obvious issues with it in Python after a cursory search though.

Re:Python stands to lose its GIL, and gain a lot of speed by byroot on Sunday October 17, 2021 @05:57PM (#28900162)
That was solved years ago by moving the refcounts into different pages: https://instagram-engineering.com/copy-on-write-friendly-pyt...
Re:Python stands to lose its GIL, and gain a lot of speed by kzrdude on Sunday October 17, 2021 @02:50PM (#28898760)
Interactive use of Python: plotting, working with data, also would benefit from better multithreading. It's interactive, so it's (a bit) frustrating to wait for it to compute and see that it uses just one thread (the statistics ops are usually well threaded already, but plotting is not).