News for hackers. Stuff that matters.
Re:The GIL and its effects on Python multithreading by zmmmmm on Wednesday September 29, 2021 @04:21AM (#28691434)
If all it was used for was scripting it would be fine, but what is strange is that Python has emerged as a major language for complex data analysis. So I am sometimes confronted with partial solutions in Python that solve a piece of the puzzle that is purely numerical (so all runs on numpy etc) but then extending it to actually solve the real problem ends up either extremely awkward (the whole architecture getting bent around Python's limitations) or being a complete rewrite. It's frustrating that the ecosystem for certain problems is so bent towards a language that is unsuitable for large parts of the job. (mainly, I would add, because it sucks the oxygen away from alternatives that would be better in the longer term).
Re:The GIL and its effects on Python multithreading by maccard on Wednesday September 29, 2021 @05:10AM (#28691694)
If you're doing complex data analysis in python, the actual number crunching isn't happening in python it's happening in a C extension. Using numpy or tensor flow, you're really just calling C code.
Re:The GIL and its effects on Python multithreading by sparsely on Wednesday September 29, 2021 @09:03AM (#28693019)
I think the OP's point was that it forces you to do all the number crunching via those extensions, and if what you want to do doesn't match up with their APIs it's a bit of a pain. Granted, they have fairly complete and flexible APIs but I can still imagine it being very annoying sometimes.
Re:The GIL and its effects on Python multithreading by xscott on Wednesday September 29, 2021 @10:28AM (#28694124)
> I think the OP's point was that it forces you to do all the number crunching via those extensions [...]

Almost. If you're using C extensions for performance, those extensions can release the GIL, and then you get parallelism in CPython. Although if you've got a bunch of small steps, Amdahl's law is going to bite you as each step needs to re-acquire the the GIL to proceed.

Note that for Numpy, I don't know which functions/methods actually bother to release the GIL, but it's possible in theory.

Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @10:53AM (#28694446)
Yes, that's exactly what's frustrating about Python.

It's not the performance per se, that doesn't really matter for Python's use cases, it's that the performance charateristics of your program are so erratic it doesn't feel like the pieces are fitting together. Denotationally equivalent operations having drastically diverging operational interpretations means that changing anything is a nightmare.

If you have an API call that does what you want, Python is basically C++. If you don't, you're paying a 100x penalty.

Most of the times performance doesn't matter, and when it doesn't, life is great. When it does matter, the situation feels worse than it actually is because optimizing Python is such a fight.

Re:The GIL and its effects on Python multithreading by mdomans on Wednesday September 29, 2021 @12:23PM (#28695897)
In general optimising Python is: * understanding that pure Python is not a language to write CPU-heavy computational code in it and was never intended as such * understanding that Python lets you write 3-4 solutions to a problem easy and 1 will be slow, two will be OK and one will be optimal - often that involves using generators, collections, sets and is a bit nuaced * I'd say that optimising Python, compared to something like C or C++ is far easier - I can set up a decent testing and profiling environment for most of my problems easy with pip and ipython which is borderline simple compared to valgrind
Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @12:39PM (#28696168)
I'm not talking about technical difficulties, the Python dev experience is top notch and I have no complaints in that area. What I mean is that it's hard to form a mental model of what's going on because it's as though you were using two different languages: the "fast" API calls and the "slow" munging of the results.

The frustrating part is that the language doesn't seem to "compose" any longer, it doesn't feel uniform: you can't really do things the simple way without paying a hefty peformance penalty.

With that said, the #1 reason why any code in any language is slow is "you're using the wrong algorithm", either explicitly or implicitly (wrong data structure, wrong SQL query, wrong caching policy, etc.), and in that sense Python definitely helps you because it makes it easier (or at least less tedious) to use the correct algorithm.

Re:The GIL and its effects on Python multithreading by kristjansson on Wednesday September 29, 2021 @05:32PM (#28700252)
I think that complaint applies to the (vast, essential) ecosystem, not to the language and standard library. AFAIK, there aren't serious performance potholes to hit in the language and standard library any more. You're not paying a huge performance penalty for writing things the simple way, but things are uniformly pretty slow.

It's when you pull in fast libraries that composition breaks down e.g. the many-fold difference between numpy's sum method and summing a numpy array with a python for-loop and an accumulator.

Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @06:38PM (#28700889)
I agree, the standard library doesn't have those problems. As you said though, the ecosystem is kind of the point of using Python in the first place.

Again, I'm not really a fan of Python as a general purpose language for large projects, but it really, really excels at being a kind of "universal glue", and it's why I use it again and again.

In that role, "uniformly slow" is better than "performance rollercoaster".

Re:The GIL and its effects on Python multithreading by mumblemumble on Wednesday September 29, 2021 @12:13PM (#28695745)
spaCy might be a good example. It's a natural language processing library that's written in Cython, so yeah, a lot of the hard computation work is essentially just calling into C code.

But it's usually being driven from a Python script that's in charge of loading the data and feeding it into spaCy's processing pipeline. That code is subject to the GIL, and, since it cannot be effectively multithreaded, it tends to become the program's bottleneck.

So what you typically end up doing instead is using multiprocessing to parallelize. But that comes with its own costs. You end up wasting a lot of computrons on inter-process communication.

Re:The GIL and its effects on Python multithreading by grumpyprole on Wednesday September 29, 2021 @06:16AM (#28691961)
And Fortran code (many parts of numpy). C doesn't have a complete monopoly on high-performance code.
Re:The GIL and its effects on Python multithreading by Qi_ on Wednesday September 29, 2021 @10:17AM (#28693981)
What is the advantage of Fortran over only C in numpy’s case specifically?
Re:The GIL and its effects on Python multithreading by staticassertion on Wednesday September 29, 2021 @10:44AM (#28694319)
Fortran has the more optimized libraries (blas) and is also capable of optimizations that C isn't (guarantees that pointers dont alias).
Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @10:58AM (#28694544)
Modern C has the restrict keyword for that. There isn't any competitive advantage left for Fortran over C or C++, the only reason why Fortran is still part of the modern numeric stack is that BLAS, LAPACK, QUADPACK and friends run the freaking world and nobody is ever going to rewrite them to C without a compelling reason to do so.
Re:The GIL and its effects on Python multithreading by mumblemumble on Wednesday September 29, 2021 @12:20PM (#28695844)
Fortran's primary competitive advantage over C and C++ is that it's a conceptually simpler language that's easier for humans to use safely and effectively for numerical computing tasks.

It's not just being tied to BLAS and friends. Fortran lives on in academia, for example, because academics don't necessarily want to put a lot of effort into chasing wild pointers or grokking the standard template library.

For my part, I'm watching LFortran with interest because, when I run up against limitations on what I can do with numpy, I'd much rather turn to Fortran than C, C++, or Rust if at all possible, because Fortran would let me get the job done in less time, and the code would likely be quite a bit more readable.

Re:The GIL and its effects on Python multithreading by staticassertion on Wednesday September 29, 2021 @12:06PM (#28695635)
I'm aware of the restrict keyword, though its usage is not typical. It enables an optimization that fortran has by default, that was my point.

And yes, I already said that packages like blas have had massive amounts of work put into them and have lasting power.

Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @12:19PM (#28695831)
I didn't downvote you, if that's what you were hinting at, however your phrasing was more likely to be interpreted as "C can't do that" as opposed to "C doesn't default to that".

It seems we agree, after all.

Re:The GIL and its effects on Python multithreading by staticassertion on Wednesday September 29, 2021 @10:04PM (#28702410)
I believe we do.
Re:The GIL and its effects on Python multithreading by adgjlsfhk1 on Wednesday September 29, 2021 @03:09PM (#28698406)
Note that while BLAS and friends aren't getting rewritten in C, there is an effort underway to write replacements in Julia. The basic reason is that metaprogramming and better optimization frameworks are making it possible to write these at a higher level where you basically specify a cost model, and generate an optimal method based on that. The big advantage is that this works for more than just standard matrix multiplies. The same framework can give you complex matrices, Integer and boolean matrices, matrices with different algebras (eg max-plus).
Re:The GIL and its effects on Python multithreading by qsort on Wednesday September 29, 2021 @03:18PM (#28698535)
That's a cool idea. I don't know how realistic it is to achieve performance parity, but the "generic" functionality is definitely intriguing.
Re:The GIL and its effects on Python multithreading by adgjlsfhk1 on Wednesday September 29, 2021 @05:22PM (#28700134)
The initial results are that libraries like LoopVectorization can already generate optimal micro-kernels, and is competitive with MKL (for square matrix-matrix multiplication) up to around size 512. With help on macro-kernel side from Octavian, Julia is able to outperform MKL for sizes up to to 1000 or so (and is about 20% slower for bigger sizes). https://github.com/JuliaLinearAlgebra/Octavian.jl.
Re:The GIL and its effects on Python multithreading by pjmlp on Wednesday September 29, 2021 @04:29PM (#28699488)
Except "modern" C does not prevent one to misuse restrict and doing so is UB.
Re:The GIL and its effects on Python multithreading by brodouevencode on Wednesday September 29, 2021 @02:11PM (#28697644)
Also, when Travis Oliphant was writing numpy he liked the Fortran implementations of a lot of mathematical functions, so it was an "easy" borrow from there instead of reimplementing.
Re:The GIL and its effects on Python multithreading by fastasucan on Wednesday September 29, 2021 @05:00AM (#28691631)
You cant both claim that it is major language for complex data analysis and that it is "unsuitable for large parts of the job". If it is a major language for complex data analysis it is apparently not unsuitable. It can be not optimal/not the best/bad but it cannot be unsuitable, because then your first statement isn't true. Which one is it?
Re:The GIL and its effects on Python multithreading by lmm on Wednesday September 29, 2021 @05:04AM (#28691659)
It's a great language for exploratory prototyping of complex data analysis and a terrible language for robust implementation of complex data analysis. Well-managed organisations prototype in Python but take the time to rewrite the prototype for production use; poorly-managed ones skip step 2.
Re:The GIL and its effects on Python multithreading by pharmakom on Wednesday September 29, 2021 @07:13AM (#28692261)
Does this seem really inefficient to anyone else? Why is there not a language that can do the prototype and offer a high performance ceiling? Julia seems like a candidate.
Re:The GIL and its effects on Python multithreading by penteract on Wednesday September 29, 2021 @11:55AM (#28695439)
There's a language design tradeoff - Python is is the result of the designers almost never saying "We should do this to make Python faster, although it would make the language slightly harder to program in."

This even goes as far as choosing not to make improvements that improve performance but make the implementation more complex (for example adding a JIT), which allows Python to continue to add language features.

Re:The GIL and its effects on Python multithreading by lmm on Wednesday September 29, 2021 @09:01PM (#28702033)
The language isn't the hard part. One of the more programming-oriented data scientists was actually happy to write Scala to start with, but honestly getting from a prototype to a production-quality implementation is pretty much a rewrite anyway (e.g. you have to consider all the error cases).
Re:The GIL and its effects on Python multithreading by FuckButtons on Wednesday September 29, 2021 @03:25PM (#28698610)
It doesn’t work like that in any other industry, why should it work for software? Mechanical engineers don’t put prototypes into production, why should software engineers?
Re:The GIL and its effects on Python multithreading by pharmakom on Wednesday September 29, 2021 @06:26PM (#28700788)
we are writing software, which is a totally different category to mechanical engineering.
Re:The GIL and its effects on Python multithreading by maccard on Wednesday September 29, 2021 @05:11AM (#28691697)
What language do you rewrite it in so?
Re:The GIL and its effects on Python multithreading by chalst on Wednesday September 29, 2021 @06:01AM (#28691906)
It's typically pretty easy to turn Python+numpy into performant Julia.
Re:The GIL and its effects on Python multithreading by rofrol on Wednesday September 29, 2021 @10:47AM (#28694367)
I remember reading this comment:

>I recently tried to migrate my R code to Julia. Even though I already knew R data.table is faster than DataFrames.jl, I was totally blown away by how slow Julia is. So I quickly gave up. I think I will have to write unavoidable hard loop in cpp, which I really don't want to do...

https://news.ycombinator.com/item?id=27404910

Re:The GIL and its effects on Python multithreading by pjmlp on Wednesday September 29, 2021 @12:33PM (#28696067)
Apparently it is working for some, https://juliacomputing.com/case-studies/
Re:The GIL and its effects on Python multithreading by lmm on Wednesday September 29, 2021 @09:02PM (#28702037)
In the case I was thinking of, mainly Scala (we were a very JVM-oriented place and used Spark clusters for our big data stuff).
Re:The GIL and its effects on Python multithreading by nick_ on Wednesday September 29, 2021 @09:54AM (#28693700)
They're saying that Python has gained a reputation for being the language for complex data analysis, but in fact it is objectively "unsuitable to large parts of the job".
Re:The GIL and its effects on Python multithreading by d0mine on Wednesday September 29, 2021 @07:02AM (#28692200)
Could you provide a specific example where GIL prevents "complex data analysis"?
Re:The GIL and its effects on Python multithreading by xscott on Wednesday September 29, 2021 @04:28AM (#28691478)
> it sucks the oxygen away from alternatives that would be better in the longer term

It's always frustrated me that the computing world isn't as it should be. Python as a language seems to have found a sweet spot for accessibility to a lot of people, but CPython the interpreter steals all the air from PyPy, which is a vastly better implementation.

Re:The GIL and its effects on Python multithreading by seunosewa on Wednesday September 29, 2021 @06:52AM (#28692142)
PyPy has a GIL too though.
Re:The GIL and its effects on Python multithreading by xscott on Wednesday September 29, 2021 @10:57AM (#28694525)
The PyPy FAQ addresses this. To my eyes, it looks like this is partly to provide the same promises CPython does for C extensions, and partly because their transactional memory approach hasn't gotten the attention it would need to succeed. I think both of those show CPython stealing the air from PyPy.

https://doc.pypy.org/en/latest/faq.html#does-pypy-have-a-gil...