News for hackers. Stuff that matters.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by omnicognate on Saturday July 08, 2023 @01:39PM (#36646697)
> existing C-API extensions will minimally need to be rebuilt and may also require other changes.

This vastly understates the work involved. Most C extensions and embeddings will require major structural changes or even rewrites. These things are everywhere and are a major reason for python's popularity. A typical financial instition, for example, will have a whole bunch of them, with a morass of python code built on top. Many of these companies took ages to transition to python 3 (and many still have pockets that haven't!). For them to remove GIL reliance from their C extensions is a much bigger ask than migrating to 3 was, and their response to being asked to do it is likely to be blunt.

The fact that only a small minority of developers involved in python directly use the C API (and fewer understand the consequences of GIL removal for them) means the issue tends to be overlooked in discussions like this, but it's the reason a GILectomy will be worse than 2 to 3. In practice it's likely to end up being either a fork or a mode you have to switch on/off, both of which would be miserable for everyone.

The GIL is part of python's success story. It made it easy to write extensions, and the extension ecosystem made the language popular. Every lanaguage doesn't have to converge to the same endpoint. Different tools are suited to different jobs. Let it be.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by floomk on Saturday July 08, 2023 @02:40PM (#36647415)
It would be helpful and interesting if you could explain why the impact is so huge
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by gary_0 on Saturday July 08, 2023 @02:51PM (#36647525)
They're talking about C code that blindly relies on implicit concurrency guarantees that are now conditional. Isn't that enough said?
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by mrlyx on Saturday July 08, 2023 @03:26PM (#36647856)
Not at all. The GIL was explicitly part of the C-API! No one used the guarantees accidentally, including CPython itself.

The GIL was one of the reasons why the C module ecosytem took off.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by gary_0 on Saturday July 08, 2023 @04:24PM (#36648342)
That's not the implicitness I'm talking about. While it sounds like loading old C modules will just re-enable the GIL, the problem is that they will never be updated to not rely on the old Python concurrency model. All that C code was written implicitly assuming that certain blocks of code were surrounded by a GIL.

It could be a real headache for any hoped-for transition to nogil Python if lots of GIL-reliant C code is floating around where there's little hope of updating it without having to worry about subtle bugs popping up. And even if the conversion was risk-free (which I doubt), many organizations will still not want to dig into their legacy C codebases and make significant changes.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by lanstin on Saturday July 08, 2023 @04:34PM (#36648454)
And the probability of a given piece of C code that was written for a thread safe environment working when called from multiple threads at once is pretty low for anything not 100% purely functional.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by CJefferson on Saturday July 08, 2023 @09:34PM (#36650603)
Honestly, that’s not my biggest worry. My bigger worry is that now Python variables your extension is reading can be changed by another thread in real time. Before if you held the GIL, you knew nothing would change while you poked some Python datastructures.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by klyrs on Saturday July 08, 2023 @03:05PM (#36647654)
> > This will be an opt-in feature by building the interpreter using `--disable-gil`. The PEP is currently targeted at Python 3.13.

> In practice it's likely to end up being either a fork or a mode you have to switch on/off, both of which would be miserable for everyone.

Yep. That's the plan.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by faitswulff on Saturday July 08, 2023 @03:16PM (#36647758)
> Yep. That's the plan.

The misery, you mean?

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by klyrs on Saturday July 08, 2023 @03:18PM (#36647782)
True Pythonistas love misery, almost as much as we love kvetching. Without the misery, we'd have nothing to talk about. Don't get me started on typing.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by lyu07282 on Saturday July 08, 2023 @10:12PM (#36650841)
Have you seen PIP, conda, asyncio from 3.4 to 3.8, the standard library documentation, the great 2 to 3 migration, type "annotations" and mypy, the variable scoping "rules" or the multiprocessing module?

We python users are more than ready for whatever pain we have to deal with in the future, we are used to misery we live it every day.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by gjulianm on Saturday July 08, 2023 @03:48PM (#36648057)
I’m not sure that most will require rewrites.

First, if the extension isn’t being used in a multithreaded environment, nothing should change. Yes, it isn’t thread safe, but it doesn’t really matter in that context. And given how bad GIL-Python works with threads, I doubt the majority of extensions are written for multithreaded applications.

And the ones that are written for multithreaded are probably already releasing the GIL for long running computations, so they should be written with at least a little bit of thread safety in mind. And in the worst case it shouldn’t be too difficult to hack the code to include a GIL-like lock that needs to be held by library users in order to ensure thread safety without really changing the architecture of the extension.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by bjourne on Saturday July 08, 2023 @07:07PM (#36649660)
There is no way of knowing whether a Python module is used in multithreaded environments or not. The point is that C extensions that previously worked fine in multithreaded environments probably will not work fine in multithreaded environments with the GIL disabled.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by schmeii on Tuesday July 11, 2023 @11:47AM (#36682659)
That is not true.

If an extension doesn't advertise that it is thread safe, the GIL get activated at runtime so it will work exactly like it does now.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by CJefferson on Saturday July 08, 2023 @09:31PM (#36650580)
In my personal experience almost every large Python program is a little multithreaded.

For example, if you need to handle the external programs input, stdout and errout, most of the examples you find by googling use threads for this.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by norir on Sunday July 09, 2023 @12:06AM (#36651409)
I'm sure you know this, but as a general psa: threads are the lowest common denominator way of doing this kind of thing. Where possible, it is nicer to use epoll/kqueue/whatever the windows equivalent is. For a library though I totally get how annoying it would be to interface with the platform event system on each os. Indeed, if there isn't already a python library that can handle subprocesses for you on a single thread then it might be something worth building (I don't use python personally so I don't know the ecosystem).
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by cpgxiii on Saturday July 08, 2023 @04:46PM (#36648580)
> Most C extensions and embeddings will require major structural changes or even rewrites.

A lot of those important "extensions" are actually C and C++ libraries designed for parallelism in their native use which have been made available in Python via bindings (e.g. Pytorch). The cores of these libraries are fine, only the binding layers (often automatically/semi-automatically generated) may need to be updated. I suspect only the C-API extensions designed from scratch to only be extensions are going to have major problems with a no-GIL world.

> Every language doesn't have to converge to the same endpoint. Different tools are suited to different jobs.

People have decided they want to use Python frontends. Plenty of alternative languages could have won out, which would have provided native threading, faster runtimes etc, but they didn't; we are stuck with Python. The continued existence of the GIL is incredibly restrictive on how you can leverage parallelism both in Python itself and in extensions. Only in the simple cases can you make the Python<->extension boundary clean: the second you want to be able to call Python from extension code (e.g. via callbacks or inheritance) the GIL stands in your way.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by 0cf8612b2e1e on Saturday July 08, 2023 @05:29PM (#36648965)
For every popular, well-engineered extension, I suspect there are a dozen hacked-together ones which will break the moment the GIL guarantees disappear.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by cpgxiii on Saturday July 08, 2023 @05:36PM (#36649013)
I'm sure there are a lot of hacky extensions out there (particularly hiding in proprietary codebases), but letting the entire future of Python be held hostage to the poor SWE choices of third parties who almost certainly contribute nothing back is not a sustainable path.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by 0cf8612b2e1e on Saturday July 08, 2023 @05:40PM (#36649040)
But it is a programming language. You do not break backwards compatibility lightly. Nobody has ever chosen Python for its runtime performance. Unfortunately, sometimes you have to accept the technical debt cannot be escaped.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by TillE on Saturday July 08, 2023 @05:48PM (#36649114)
Stuff breaks in software all the time, it's really not a huge deal. You either update your code or stick to an older version.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by cpgxiii on Saturday July 08, 2023 @06:26PM (#36649389)
> Nobody has ever chosen Python for its runtime performance.

No, they choose it for the ease of using its performant extensions. And those extensions are fundamentally limited in performance by the existence of the GIL. And the authors of those extensions (and their employers) are behind the work to get rid of it.

There are three groups here:

1. "Pure" Python users, whose code makes little/no use of extensions. GIL removal is an immediate win for these uses.

2. "Good" extension users/authors, whose code will support and benefit from no-GIL. GIL removal is an immediate win for these uses.

3. "Bad" extension users/authors, whose code doesn't/can't support no-GIL. GIL removal probably doesn't break existing code, but makes new uses potentially unsafe.

Maintaining the technical debt of the GIL indefinitely so that group 3 never needs to address its own technical debt is not a good tradeoff for groups 1 and 2 which actively want to move the language forwards.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by 0cf8612b2e1e on Saturday July 08, 2023 @07:53PM (#36649938)
Don’t break user space.

The above arguments could apply to any breaking changes that a host wants to implement. Those who don’t directly consume the change, those who can adapt, and everyone who is fine with the status quo.

Python is free to do as it pleases, but this breaking change is going to result in a lot of churn.

> GIL removal is an immediate win for these uses.

As a minor aside, the GIL free Python version was actually a performance regression, somewhere between 5-10% impact. https://peps.python.org/pep-0703/#performance . So, an immediate loss for nearly everyone.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by omnicognate on Saturday July 08, 2023 @07:59PM (#36649978)
No, this is false. Relying on the GIL isn't bad practice in a python extension. It's something you have no choice but to do. The GIL is the thread safety guarantee given to the extension by the python intepreter. It's the contract you have and you have no choice but to code to it. Removal of the GIL requires an alternative contract representing a finer-grained set of thread-safety guarantees for people to code against. Programmers who coded against the contract that existed rather than somehow divining the future and coding against a different contract that hadn't been invented yet (while also managing to make it compatible with the existing one) weren't doing something wrong.

But besides being incorrect, the idea that extensions that break on removal of the GIL are "bad" is irrelevant. The question is what the ecosystem will bear. The people who own the codebases I'm talking about are not pushing for GIL removal. They have large, working codebases they're using in production and the cost of redesigning large chunks of them would be real. GILectomy is being pushed by people with varied motives but they certainly don't speak for everyone and their case isn't going to be made successfully by casting aspersions on those with different priorities.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by rmahrt on Sunday July 09, 2023 @05:38AM (#36653039)
Relying on a published guarantee of CPython is not technical debt!

What will happen is that extensions that don't work with the new behavior will be assimilated by the proponents. Support for the new behavior will be hacked in, probably with many mistakes.

The bad results will then be marketed as progress.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by fragmede on Sunday July 09, 2023 @05:51AM (#36653091)
it is now! just like that bill you didn't know you owed until it came in the mail, technical debt builds up regardless of when you know when it's owed.

keeping a gil when entering and exiting c extensions seems like an obvious steppingstone.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by Attummm on Sunday July 09, 2023 @06:01AM (#36653140)
Would it be possible to define "win" for your point 1 and 2?
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by josefx on Saturday July 08, 2023 @08:51PM (#36650304)
> Nobody has ever chosen Python for its runtime performance.

How many people do you think would complain if the python devs. replaced numpy and pythorch with pure pyhton implementations for an April fools joke?

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by kelnos on Saturday July 08, 2023 @09:23PM (#36650521)
That doesn't contradict what the GP said. Those modules are largely written in C, not Python. And that is because nobody would choose Python for its performance.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by josefx on Sunday July 09, 2023 @03:45AM (#36652500)
> And that is because nobody would choose Python for its performance.

Python the runtime may not be the most performant by itself, but python the ecosystem only took of because of performance hacks like those popular modules, pypi, cython, ... .

> Those modules are largely written in C, not Python.

Yet people choose C to implement those modules because Python is not performant enough and isn't that a nice thought? Lets do something good for humanity and drive another stake into that old abominations heart by making it less relevant.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by toyg on Sunday July 09, 2023 @10:22AM (#36654666)
What? Pypy and cython are hardly the reason for python "taking off".

The issue with python is that the ecosystem is so big, everyone thinks their niche is the one python was "built for". Data science, web backends, sysadmin, app scripting, superclusters, embedding... They all think they're the biggest dog, when the reality is that they're more or less all the same. Pypy and cython are probably popular in some of those niches, but python is not just about them.

The reality is that python "took off" because of two things:

- the syntax

- the ease of interfacing with other worlds, typically (but not limited to) C/C++

Removing the GIL seriously threatens the second reason, because integrations that used to be fairly easy will have to be fundamentally rearchitected. Moving from Python 2 to 3 was trivial in comparison, and it took a decade; this transition might take much longer, or never really happen, with massive losses for the ecosystem. And all for what, some purist crusade that interests only a fraction of the whole community...?

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by bsder on Saturday July 08, 2023 @05:26PM (#36648940)
> This vastly understates the work involved. Most C extensions and embeddings will require major structural changes or even rewrites.

Why?

Once you have a threaded Python, you can allocate one thread to run with a GIL and the other threads without it. Old extensions can access one thread with the old GIL API and new extensions can access all the things with the GIL-less API.

People who want the performance will rewrite their extension. People who don't, won't.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by tsukikage on Saturday July 08, 2023 @06:08PM (#36649276)
That's not quite good enough - people use threads to e.g. permit multiple blocking i/o operations to be in flight at once, which works perfectly well right now. So you're going to want to support multiple GIL threads if you want to be able to support existing code safely.

Sadly, you can't just trivially bolt on thread safety to code that was not designed with it in mind.

However, if you go the other way and introduce e.g. an @nogil decorator, similar to that seen in some cpython alternatives, people will have a straightforward path to opt in incrementally as they fix and verify the critical parts of their code, while preserving known working behaviour elsewhere without having to throw entire complex systems over the wall at once.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by schmeii on Tuesday July 11, 2023 @12:30PM (#36683371)
I don't think that is possible to activate the GIL only for one thread, it has to be activated process-wide.

But, if you want free-threading but you have to use an old extension that is not GIL-less, then you still can, by garding all access to this extension with a lock and setting the PYTHONGIL variable to 0.

But, you will need to compile the extension with --disable-gil or to obtain that from somewhere, if the extension author doesn't provide it, as the ABI of "standard" python and "--disable-gil" python is different.

How --disable-gil builds of python and of extensions will be distributed is not yet totally clear. I don't think pypi has already a clear idea of how to handle those extension, for instance.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by jtokoph on Saturday July 08, 2023 @07:43PM (#36649880)
Could there be a shim between the two that emulates the original behavior?
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by schmeii on Tuesday July 11, 2023 @11:55AM (#36682806)
There already is such a mechanism in the pep.

Basically there will be two build mode.

The standard build that you get at python.org will work exactly as now (so with a Gil).

The disable-gil build (which will most likely be available through conda or similar) will have the option to run either with the GIL or without. It will automatically detect extension that are not compatible and switch to Gil mode but you can override this with an environment variable.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by dataflow on Saturday July 08, 2023 @08:51PM (#36650302)
> A typical financial institution

Could you elaborate on how the Python ecosystem will suffer if these financial institutions get left behind? What sorts of contributions come from these users in particular?

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by paulddraper on Saturday July 08, 2023 @09:43PM (#36650672)
Python will lose marketshare/mindshare.

StackOverflow answers, side projects, fanboys

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by rmbyrro on Saturday July 08, 2023 @09:58PM (#36650760)
Did you read the part that you'll have to opt in and build the interpreter with --disable-gil?

If it doesn't work for you, use the GIL enabled. What's the problem of letting others disable it?

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by wokwokwok on Saturday July 08, 2023 @11:13PM (#36651137)
Come now, how many people compile python to use it?

How many people will be surprised / upset that they have to do that once `--disable-gil` is mainlined into the default builds on python.org?

How long before 'build it yourself and use --disable-gil' becomes 'we've enabled --disable-gil by default for Great Victory for Most Users'?

I think it's very very naive to think that the flag will remain an obscure 'use if you want to build python yourself' if the PEP is successful.

> The global interpreter lock will remain the default for CPython builds and python.org downloads.

Is a joke. It means, for now. For the work in this PEP.

I mean, you've literally got the folk from numpy saying:

> Coordinating on APIs and design decisions to control parallelism is still a major amount of work, and one of the harder challenges across the PyData ecosystem. It would have looked a lot different (better, easier) without a GIL.

That isn't 'for a few people who are vaguely interested in obscure technical stuff'; this is the entire python data ecosystem, saying "this will be the default in the future because the GIL is a massive pain in the ass".

> If it doesn't work for you, use the GIL enabled.

Sure, it can be opt-out; but you're fooling yourself if you think this is going to be 'opt-in'.

It will be the default once it's completed, sooner or later, imo.

...and when the 'default' build of python breaks c-extensions, that is a breaking change, even if technically you can build your own copy of python with different compile flags.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by AlphaSite on Saturday July 08, 2023 @11:19PM (#36651163)
It will probably be something like:

    brew install [email protected]
    apt install python3.12-mp
Etc. I don’t think anyone actually needs to compile it themselves, unless they really want to.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by wokwokwok on Sunday July 09, 2023 @01:21AM (#36651783)
Long term? I doubt it. Who’s going build those packages?

Two major parallel incompatible implementations of Python, maintained at the same time, duplicating all efforts at testing, dev, build, release train, etc. while both are maintained.

Does it sound familiar?

Even ignoring the obvious parallels to Python 3000, there’s a limited amount of time people will be bothered maintaining both.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by schmeii on Tuesday July 11, 2023 @12:18PM (#36683162)
Probably in the long-term the two build-mode will be merged and there will only be a "nogil" build-mode.

Which doesn't mean there will be no GIL. Contrary to popular belief the goal of PEP 703 is not to remove the GIL, but only to be able to disable at runtime if you need free threading, so it is an opt-in feature.

In the interim, it seems that Conda have volonteered to build the extensions that are frequently used in the scientific community.

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by dzsekijo on Sunday July 09, 2023 @06:50AM (#36653396)
However, these days AI assisted tools can be involved in the task.
Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by js2 on Sunday July 09, 2023 @06:46PM (#36659814)
> Most C extensions and embeddings will require major structural changes or even rewrites.

Sam claims otherwise: "Most C API extensions don’t require any changes, and for those that do require changes, the changes are small. For example, for “nogil” Python I’m providing binary wheels for ~35 extensions that are slow or difficult to build from source and only about seven projects required code changes (PyTorch, pybind11, Cython, numpy, scikit-learn, viztracer, pyo3). For four of those projects, the code changes have already been contributed and adopted upstream. For comparison, many of those same projects also frequently require changes to support minor releases of CPython."

https://discuss.python.org/t/pep-703-making-the-global-inter...

In the following talk, he shows the scikit-learn changes that were needed:

https://youtu.be/9OOJcTp8dqE?t=1958

I'd also like to point out that the GIL does not inherently make C extensions thread safe. It only protects the interpreter itself. For example, calling in to the C API can already release the GIL as I explained in reply to /u/bjourne here:

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

Re:Meta pledges Three-Year sponsorship for Python if GIL rem... by semi on Monday July 10, 2023 @11:27AM (#36667837)
would a fork be so bad? maybe I'm off but I feel like most people have no real need for GILless and would leave it off if they were even aware it existed. Those who would need it the most are the most equipped to handle the cost of adoption.