Live data from Hacker News

The Changing "Guarantees" Given by Python's Global Interpreter Lock

stefan-marr.de

81–90 of 142 posts

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#81
post #2

Code that assumes that something is going to be atomic because of the GIL (or any other implementation detail) is simply broken. If you need something to be atomic you should be explicit about that and use mutex or something.

Most people and scripts using the GIL to ensure safety likely don't even realize it is necessary. That is the hard part of this type of migration. Decades of code written with the implicit assumption of the GIL for all situations.

I don't think people generally write code to use this or another guarantee of the implementation. They write code, they run it, it seems to work (on my machine) and that's it. Later, the implementation changes in a subtle way (or someone tries to run the code in an incompatible version) and the code stops working. Nothing to do with "relying on GIL". The same may apply to relying on dict key order, timing of things, or anything else. That's why we have race conditions, Docker images, freezing dependencies, "only works in IE", etc.

Writing to spec is very rare in my experience. You usually learn the spec once someone tells you your code doesn't work on XYZ.

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#82

Earlier quoted context omitted.

Relying on implementation behavior, even if it’s very visible and hasn’t changed for a few decades, is morally wrong and reprehensible. Only the letter of the standard has any bearing on correct reality.

> morally wrong That's a wild one

woe is man who loses sight of god and sees only himself

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#83
post #25

Earlier quoted context omitted.

Just because everybody does something the wrong way doesn’t somehow make it magically correct. One study [1] in the US released in 2020 found almost 90% of people admitted to speeding, but I don’t think anyone would say that speeding is now approved by the authorities and consequence-free. [1] https://www.thezebra.com/resources/research/speeding-car-ins...

Just because everybody does something the wrong way doesn’t somehow make it magically correct. Hyrum's Law says the opposite: https://www.hyrumslaw.com/ "With a sufficient number of users of an API, it does not matter what you promise in the contract: all observable behaviors of your system will be depended on by somebody." Named after some guy called Hyrum who worked / works at Google.

Agreed. It's also the "the first rule of kernel maintenance," according to Linus: https://linuxreviews.org/WE_DO_NOT_BREAK_USERSPACE

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#84

It seems like in every python discussion I hear people complain about the GIL. I’m happy people are working on removing the GIL, but As a professional python dev for about 5 years now I have literally never had a problem where the GIL was a limiter. Although I just make web apps so maybe I’m not the target audience.

Conversely, I've worked on backend, data processing-type applications for most of my career, much of it in Java but some (especially recently) in Python, and the GIL is a huge limiting factor for writing efficient, readable Python code. I've had to write very annoying Python code using the multiprocessing library to get around the GIL, and ultimately it works, but it's ugly and clunky and overall just a pain. And rem…

I think multiprocessing is quite sensible in Python (comparing to async for an example)

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#85
post #60

Earlier quoted context omitted.

Some languages just use integer versions, like Java.

Java dropped the leading 1 with version 5 since Sun decided it would never go for a complete rewrite of the language. Imagine the chaos if Python did that and then pulled the 2 to 3 change on a run of the mill update from 213 to 214.

Not sure that's entirely accurate.

Java 1.2 was branded as "Java 2", so you had the J2SE (Java 2, Standard Edition) and related J2ME and J2EE (Mobile and Enterprise, respectively) platforms. The "Java 2" moniker was dropped in Java 5, which was the largest rewrite of the language since, adding generics, sane memory model, annotations, etc., all in the same language revision.

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#86
>> I have at least one very concrete example of code that someone assumed to be atomic:

request_id = self._next_id

self._next_id += 1

To think that is thread safe is just naive. Once you understand the potential problem you can look at the code and ask "why shouldn't this be unsafe?" Since there is nothing explicitly preventing the problem. After reading TFA (lazily I admit) I still don't know why that code is thread-safe with the GIL.

Python is fun and often forgiving, but a bunch of people who got lucky (because they were never taught about the hazards) are going to learn some new stuff with no-gil. I think it's a long overdue change and worth the (single thread) performance hit and bug surfacing phase.

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#87
post #14
post #9

these examples of "what one would assume to be atomic" did not seem useful to me, they looked like things that are obviously not threadsafe. a more interesting example is something like this: # setup l = [] # thread A l.extend([1, 2, 3]) # thread B l.extend([4, 5, 6]) is the resulting list always within the set of [1,2,3,4,5,6] or [4,5,6,1,2,3] ? or are the two sets of numbers randomly interleaved in the list? or if…

I'm guessing interleaving is probably not possible because the `extend` call is passed the full list, and it holds the GIL until it's finished. But you'd have to look at the bytecode to be sure, I suppose. If there were three append calls instead of a single extend, I would assume any interleaving would be valid. > or if the GIL is removed does the interpreter segfault (I'm pretty sure this latter will not be the cas…

Let's say you're passing 1 million ints to each extend. One may start wondering if those operations are then split into chunks...

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#88

It seems like in every python discussion I hear people complain about the GIL. I’m happy people are working on removing the GIL, but As a professional python dev for about 5 years now I have literally never had a problem where the GIL was a limiter. Although I just make web apps so maybe I’m not the target audience.

I’ve had the same experience mostly writing web apps in Ruby, which locks similarly to Python. Multi-threading is always needed to saturate IO, network, etc, never CPU, so GIL isn’t an issue. I always wondered what people were doing that ran into issues.

With Python being the language of choice for ML workloads I guess it’s more common to have the CPU be a bottleneck. It seems cool they’re making an option to turn it off for those use cases.

It seems like Python could maintain a GIL compatibility option to preserve the current/old behavior for legacy code.

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#89
post #2

Code that assumes that something is going to be atomic because of the GIL (or any other implementation detail) is simply broken. If you need something to be atomic you should be explicit about that and use mutex or something.

Python doesn't have a standards document. It has a reference implementation that defines the language. IOW, the python implementation is the standard, and therefore any code that relies on an implementation detail in the reference implementation is, by definition, correct.

> Python doesn't have a standards document. It has a reference implementation that defines the language.

That's not quite strictly true, Python does have a documented "Language Reference": https://docs.python.org/3/reference/index.html

If there is a contradiction between the Language Reference and CPython then one, or both, of them needs to be updated and it's treated on a case by case basis.

If an alternative Python implementation follows the Language Reference but chooses different details outside it, that doesn't stop it from being "Python". Of course practically speaking most alternative implementations are incentivized to closely follow CPython.

Re: The Changing "Guarantees" Given by Python's Global Interpreter Lock

#90
post #74

Earlier quoted context omitted.

The introduction section states that it's not a complete/exact specification. If you're using a Python implementation and have a question, this should answer it. If you're writing a Python implementation and have a question, this may not answer it. > Consequently, if you were coming from Mars and tried to re-implement Python from this document alone, you might have to guess things and in fact you would probably end u…

> if you were coming from Mars and tried to re-implement Python from this document alone, you might have to guess things and in fact you would probably end up implementing quite a different language Yes, I know that statement is in the Introduction, but I think it's rather ill-considered. If my implementation is consistent with the language reference, on what grounds would someone claim it was not an implementation o…

On the grounds that we live in the real world, not a world where anything written down is a true and complete description of reality. "Technically correct" is sometimes a synonym of "incorrect".
Post reply on HN