|
|
Subscribe / Log in / New account

Progress on no-GIL CPython

Did you know...?

LWN.net is a subscriber-supported publication; we rely on subscribers to keep the entire operation going. Please help out by buying a subscription and keeping LWN on the net.

By Jake Edge
October 10, 2023

Back at the end of July, the Python steering council announced its intention to approve the proposal to make the global interpreter lock (GIL) optional over the next few Python releases. The details of that acceptance are still being decided on, but work on the feature is proceeding—in discussion form at least. Beyond that, though, there are efforts underway to solve that hardest of problems in computer science, naming, for the no-GIL version.

ABI concerns

In mid-September, Sam Gross, who authored PEP 703 ("Making the Global Interpreter Lock Optional in CPython"), posted a message to the Python discussion forum about the interaction of the PEP and the CPython stable ABI. In part, the stable ABI is used by extensions to enable their binary wheels to work with multiple CPython versions, thus not require rebuilds when CPython is released. The PEP envisions a path toward an eventual single CPython version without the GIL, but, in the meantime, there will be a build of the interpreter (using ‑‑disable‑gil) that can be used to test no-GIL operation.

Gross noted that extensions built for the stable ABI will not actually work with a no-GIL CPython 3.13 (which is due in October 2024), but he is proposing some changes so that extensions will work with both types of CPython builds from that version onward. Extensions that only call into the "limited" C API result in binaries that use the stable ABI, so Gross suggested a few additions and changes to the API in order to facilitate extension binaries that can work with both interpreter types (GIL and no-GIL). In part, it adopts the existing plan to switch some macros to function calls, in particular for incrementing and decrementing the object reference counts that are used for garbage collection.

Victor Stinner, who has done a lot of recent work on the stable ABI, replied that he thought there should a simple solution for extensions that can work on both interpreter types; he is concerned that the no-GIL experiment will fail otherwise. Because extensions that are built for the stable ABI on CPython 3.12 and earlier will not be compatible with no-GIL builds from 3.13 onward, he thinks it may make sense to create a new ABI version to differentiate the cases. Currently, the stable ABI is "abi3", but that could be bumped to abi4—even without moving to a Python 4. Many people seem to believe the ABI number and CPython major number are linked, but that is not the case.

Gross is less concerned about the need for extensions that want to support no-GIL mode having to build two separate binary wheel versions as he described in his original message. He believes that building two wheels is a manageable price to pay and is more worried about tying the no-GIL project to fixes or upgrades to the C API and stable ABI. Alex Gaynor agreed about the price; he has multiple packages with abi3 wheels (and "is very excited about the glorious nogil future") that would be affected, but creating two wheels is not overly burdensome as a one-time thing. He does want to ensure that existing and future versions of pip do the right thing in choosing between them, however.

Brett Cannon said that existing and older versions of pip would not do the right thing, unless a change to abi4 was made; the logic used in pip today would not be able to distinguish between the versions. Gross suggested that supporting older pip versions may not really be needed, since it is not an actual change in behavior:

I don't think we should worry too much about whether old versions of pip work properly for the experimental, --disable-gil build of CPython 3.13. As it is, old versions of pip frequently do not work with new versions of Python. For example, pip==23.1.1 and older (from just 5 months ago) will break if installed in CPython 3.13 (missing pkgutil.ImpImporter).

pip maintainer Paul Moore pointed out a difference, though: breakage is different from silently doing the wrong thing:

I think it's fine if an older version breaks for newer Python. But I'm less sure that silently installing the wrong package is acceptable. People do use older versions of pip, and loud breakages aren't the same as silent errors.

He noted that pip has a policy that users should always upgrade to the latest version, but that the project has no specific policy on silent errors of this sort. He is concerned that those who want to experiment with the no-GIL (or "free-threaded") builds will be put off if they end up "having to debug ABI compatibility issues like this". Gaynor agreed, noting that "pip silently doing the wrong thing creates a flood of issues" for the packages affected.

Barry Warsaw asked about whether there were plans for allowing installation of both interpreter types on the same system. Gross said that the situation was the same as having two different versions of Python installed. Warsaw thought that was reasonable and that it is not too difficult handle parallel installations. In the message linked above, Cannon said that one solution could be a "fat" wheel that had both binaries, as long as the names of those binaries in the wheel were distinct.

Naming

But the naming discussion got spun off into its own thread—once it clearly headed in that direction, anyway. Moore said that it was important to be able to install both interpreters so that people can easily test the no-GIL mode; if that is not straightforward to do, in Windows, macOS, Linux, and others, it will negatively impact the no-GIL project:

This links back to the packaging question, because how easy it is for users to try out nogil builds, and how easy it is for a user to select between nogil and gil, will strongly affect how much demand there is for nogil builds, and hence how much pressure there will be on package maintainers to provide nogil-compatible wheels in the first place.

Barry Scott wondered what names would be used, noting that the "shebang line" (i.e. "#!/usr/bin/python" or similar at the top of a script) needs to indicate which interpreter to invoke, which should the same across all of the platforms if possible:

The gil version of python executable is "python3". What is the name of the nogil executable? "python-nogil3", "python-nogil3.13" etc?

But the no-GIL build for 3.13 is clearly meant as an experimental feature, Gregory P. Smith said, which means that distributions should not be putting that build on the default $PATH, at least in his opinion. Smith is a member of the steering council, but noted that he was using his "personal reply hat". Having a lengthy name for the interpreter that lingers for long periods afterward because it remains in shebang lines is not desirable, so he suggested waiting:

In effect this defers any potential install naming decision until 3.14 or later once we've got some practical knowledge of how things are working out - we'll presumably know better at that time if there's a need for parallel installs. It's okay for some things like this to be decided later, in effect that is why PEP-703 has an Open Issues section not fully specifying this area today.

But Fedora developer Petr Viktorin pointed out that distributions are likely to want to package the no-GIL interpreter for their users to experiment with. Moore agreed that users are going to want that:

I would like to be able to write scripts with #!/usr/bin/env python3.13-nogil (or something like that) in order to get the free threaded build without needing to hard-code a long and probably non-intuitive path (which on Windows is also user-specific).

In another thread that was started by Steve Dower, who creates the official Python packages for Windows, Smith noted that the steering council has agreed that it wants to avoid the name "nogil" to describe the build. With his council hat on, he said that there were two reasons behind this decision: it "does not communicate well to most non-core devs, people need not know what a GIL is" and it contains a negative. There is a suggested alternative:

A more appropriate term for this experimental in 3.13 build if you have a need to provide builds is "free-threading". We realize "free-threading" doesn't roll off the tongue like two-syllable "nogil" does, but it should be more understandable to people-who-are-not-us.

That set off some predictable bikeshedding on naming, with several in the thread noting that they think "nogil" is the better choice—or at least that they are perfectly happy with that name. Gross thought that the suggested alternative was confusing: "While I understand the objections to 'nogil', I don't think 'free-threading' is likely to be understood by people-who-are-not-us." He noted that the term is not widely used elsewhere. Most participants generally just wanted to use something short—"nogil" was the clear "winner" in that department—but none of the suggestions made there seemed to resonate. The one concrete change was to switch the ABI tag for the no-GIL builds from "n" to "t" (for "threading"). Excising "nogil" at this point is going to be something of an uphill climb, it would seem.

Proposing abi4

Back in the original thread, there was some discussion, mostly between Viktorin and Gross, about problem areas in the API changes that Gross proposed. That led to a new proposal that incorporated the feedback and adopted the idea of creating a new ABI, abi4. Gross has developed a prototype of the new ABI. Viktorin is generally in agreement with the approach, though some details still need to be worked out.

One of those details is that a PEP for abi4 is needed, as Stinner pointed out. Viktorin agreed ("This is a pre-PEP discussion.") and it would seem that the mid-October core developer sprint will be the venue for some face-to-face discussions on the topic. In particular, there is confusion about the compatibility guarantees provided by the combinations of various limited API versions and abi3, which will impact what will happen with abi4. Research on those issues is ongoing.

So, work on the no-GIL (or free-threaded) version of CPython is proceeding apace, but the final acceptance of the PEP is still pending. It is somewhat surprising that there has been such a lengthy delay, but PEP 703 and its knock-on effects are likely to dominate CPython development—and ecosystem—over the next five or more years, so the steering council wants to make its acceptance criteria clear. As council member Thomas Wouters put it: "we're ironing out the exact acceptance text (we want to clarify a lot of our decisions)". Some of that work may happen at the sprint as well.


Index entries for this article
PythonC API
PythonGlobal interpreter lock (GIL)


(Log in to post comments)

Progress on no-GIL CPython

Posted Oct 10, 2023 21:37 UTC (Tue) by TheGopher (subscriber, #59256) [Link]

Why not just python4 then? Seems like a logical cut-off point.

Progress on no-GIL CPython

Posted Oct 10, 2023 22:09 UTC (Tue) by ringerc (subscriber, #3071) [Link]

Probably because it doesn't actually directly affect most Python code. Unless you're using the C API it's (generally) not going to impact you much, if at all.

Progress on no-GIL CPython

Posted Oct 11, 2023 14:15 UTC (Wed) by ericproberts (subscriber, #139553) [Link]

It's a major backwards-incompatible change given the number of python libraries written in C. A major version increase would indicate python users might need to do some extra work to upgrade, even if just bumping up dependency versions -- and, let's be real, some libraries will just fall out of use instead of become nogil-compatible, so upgrading won't *necessarily* be straightforward. No need to pretend that isn't what this is.

And there's nothing wrong with major version upgrades that require most users to do some work to upgrade. It's just the previous upgrade made python scared of its own shadow.

Progress on no-GIL CPython

Posted Oct 11, 2023 16:28 UTC (Wed) by NYKevin (subscriber, #129325) [Link]

IMHO it's not time for that *yet.* A major version bump will, rightly or wrongly, be interpreted as a promise that nogil will definitely land in mainline at some point in the near future, and that doing so will necessarily break backcompat. Based on how it is described in this article, I don't think the work is far enough along yet to make either of those promises:

* It may turn out that nogil only offers a performance win in a narrow set of circumstances that are atypical of most Python code, and so nogil Python becomes a separate, non-default mode of the interpreter, and relatively few programs actually use it in practice.
* It may turn out that a future version of the interpreter is able to create some sort of coarse-grained locking scheme that is "good enough" to be backwards compatible with "most" C extensions, perhaps requiring at most a simple recompile.
* It may turn out that nogil is so costly and infeasible that the work is dropped entirely. It doesn't currently look like it's trending in that direction, but development timelines are notoriously hard to predict.
* It may turn out that a major version bump is the best way to express nogil's effect on the Python ecosystem, but they can cross that bridge when they come to it.

Progress on no-GIL CPython

Posted Oct 11, 2023 18:34 UTC (Wed) by iabervon (subscriber, #722) [Link]

It seems entirely plausible that every C module written for 3.5 and recompiled for each later version will end up working fine with a Python that makes the no-gil implementation the normal one, but that loading such a module makes multithreaded performance even worse than 3.5 with respect to contention, due to causing everything to require a global and less subtle lock. On the other hand, programs using libraries not updated to deal well with the new interpreter probably don't use threads and don't care.

Progress on no-GIL CPython

Posted Oct 11, 2023 19:20 UTC (Wed) by NYKevin (subscriber, #129325) [Link]

> [...] but that loading such a module makes multithreaded performance even worse than 3.5 with respect to contention, due to causing everything to require a global and less subtle lock.

That is not a compatibility break. That is a quality of implementation issue. If the code still works and gives the right answer in a somewhat-believable* amount of time, then it is compatible. The question is whether this is a good or a bad tradeoff to make, not whether it will break anything. Offering two versions of the interpreter (or better yet, a flag/environment variable) would be one way to mitigate that issue, but they might instead reasonably decide that maintaining both versions is not worth the effort.

* I'm not going to precisely define "somewhat believable amount of time," but if performance on a modern system is comparable to the previous version on hardware released within the 21st century, then that's probably good enough. If it runs like a PDP-11, then that's probably too slow.

Progress on no-GIL CPython

Posted Oct 11, 2023 8:20 UTC (Wed) by danielthompson (subscriber, #97243) [Link]

nogil is essentially a change to the CPython implementation rather than the Python language. Adopting python4 would imply for many people that the releases included major incompatible language changes similar to what happened in python2 to python3.

Progress on no-GIL CPython

Posted Oct 13, 2023 4:31 UTC (Fri) by stephanlachnit (subscriber, #151361) [Link]

Just because this was the case, doesn't mean that has to be true for Python 4 as well. Python 3 has removed stdlib modules, which is already a breaking change.
While I agree that tagging a new version without some testing is not helpful, but I hope that they eventually just tag it Python 4.

Progress on no-GIL CPython

Posted Oct 13, 2023 9:15 UTC (Fri) by NRArnot (subscriber, #3033) [Link]

Precisely my first thought. Python4 should be defined as python-nogil. The Python language is the same for both versions, and future language additions would stay in step i.e. python3.15 and python4.15 would always run the same pure Python code without issues.

BUT using a different abi, so python3 libraries won't necessarily be available under python4 to start with (or sometimes, never).

A long, LONG way down the road, python3 might be retired, because there's nothing significant left that python4 can't do.

Progress on no-GIL CPython

Posted Oct 20, 2023 22:34 UTC (Fri) by jonaso95 (guest, #121422) [Link]

People are absolutely traumatized from the python2->3 transition - any major version bump will be perceived as a massive, nerve-wrecking, never ending migration process (even though in reality it wouldn't be)

Progress on no-GIL CPython

Posted Oct 11, 2023 0:57 UTC (Wed) by csigler (subscriber, #1224) [Link]

Obviously, the proper code name should be "Gunderson":

https://simpsons.fandom.com/wiki/Gil_Gunderson

As to an official name, that's above my pay grade.

Progress on no-GIL CPython

Posted Oct 11, 2023 5:58 UTC (Wed) by jengelh (subscriber, #33263) [Link]

Nah, "not gill" is quite in line with Python tradition.

Progress on no-GIL CPython

Posted Oct 20, 2023 12:52 UTC (Fri) by sammythesnake (guest, #17693) [Link]

Or indeed "not-GIL-T" (with the T standing for "threading") :-D

Progress on no-GIL CPython

Posted Oct 11, 2023 6:17 UTC (Wed) by iabervon (subscriber, #722) [Link]

Or Strang, who got a two month head start on Gil retirement.

Progress on no-GIL CPython

Posted Oct 12, 2023 4:32 UTC (Thu) by carlosrodfern (subscriber, #166486) [Link]

python-gilless!!

Progress on no-GIL CPython

Posted Oct 21, 2023 13:34 UTC (Sat) by remow (guest, #167576) [Link]

Python-unlocked!

Progress on no-GIL CPython

Posted Oct 13, 2023 8:46 UTC (Fri) by tsr2 (subscriber, #4293) [Link]

Would it be helpful to have the name flag that this is an experimental version? Abbreviating no gil to ng would allow users who don't understand no gil to assume it's next generation. That give us the name python-expt-ng.

Progress on no-GIL CPython

Posted Oct 19, 2023 20:01 UTC (Thu) by jepsis (guest, #130218) [Link]

Considering renaming 'nogil' to 'ng' would be a good idea. It also hints at 'next generation', which remains relevant.

Progress on no-GIL CPython

Posted Oct 21, 2023 9:48 UTC (Sat) by mirek (guest, #85311) [Link]

Use -ng suffix.
No-Gil for people who don't mind.
Next-Gen for people who don't like negatives.

Progress on no-GIL CPython

Posted Oct 27, 2023 17:20 UTC (Fri) by estansvik (subscriber, #127963) [Link]

Easy, Python = GIL is on, Pythoff = GIL is off


Copyright © 2023, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds