Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-119241: Add HOWTO for free-threaded C API extensions #119877

Merged
merged 5 commits into from
Jun 18, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented May 31, 2024

@colesbury
Copy link
Contributor Author

There is more I'd like to add to the HOWTO, such as information on critical sections or other synchronization primitives if/when those APIs become public, but I think that even in the current form will be useful.

cc @ngoldbaum

@ncoghlan
Copy link
Contributor

ncoghlan commented May 31, 2024

Question that came to mind when reading was whether there is a way to compile a single binary extension that is usable on both free threaded and GIL protected builds (if the latter are still looking for an official name, I offer that as a suggestion).

Also (if that is possible) how to check at runtime whether the GIL is enabled or not.

The content that is there looks good, though!

@ncoghlan
Copy link
Contributor

Direct link to the preview: https://cpython-previews--119877.org.readthedocs.build/en/119877/howto/free-threading-extensions.html

@colesbury
Copy link
Contributor Author

...whether there is a way to compile a single binary extension that is usable on both free threaded and GIL protected builds

No, that's not currently possible. That's something we hope to address in 3.14 with support for the limited API and stable ABI.

@da-woods
Copy link
Contributor

da-woods commented Jun 1, 2024

I don't know if this is the right place to document it but it might be:

If you use the Windows installer then you end up with one copy of pyconfig.h that doesn't have Py_GIL_DISABLED set. What that means is that running something like python313t setup.py build_ext --inplace will generate an extension that's named as if it's a free-threading extension (e.g. something.cp313t-win_amd64.pyd) but actually isn't. This will crash on import.

The upshot is that people building extensions on Windows will most likely have to manually define Py_GIL_DISABLED themselves.

Most of the C API is thread-safe, but there are some exceptions.

* **Struct Fields**: Accessing struct fields directly is not thread-safe if
the field may be concurrently modified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clarify that you're talking about C structs provided by the CPython C API.

`setuptools <https://setuptools.pypa.io/en/latest/setuptools.html>`_ to build
your extension and currently set ``py_limited_api=True`` you can use
``py_limited_api=not sysconfig.get_config_var("Py_GIL_DISABLED")`` to opt out
of the limited API when building with the free-threaded build.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding a note that this implies that they'll need to set up wheel builds specifically for the free-threaded build if they want to ship free-threaded binaries and otherwise rely on the limited API to only produce a single wheel per platform.

Doc/howto/free-threading-extensions.rst Show resolved Hide resolved
@colesbury
Copy link
Contributor Author

I've updated the docs with the above feedback and moved some things into a "Building Extensions for the Free-Threaded Build" section.

@da-woods
Copy link
Contributor

da-woods commented Jun 6, 2024

Looks good to me (but I doubt it's my review that you're waiting on)

Doc/howto/free-threading-extensions.rst Outdated Show resolved Hide resolved
Doc/howto/free-threading-extensions.rst Outdated Show resolved Hide resolved
Doc/howto/free-threading-extensions.rst Outdated Show resolved Hide resolved
@colesbury colesbury merged commit 02b272b into python:main Jun 18, 2024
27 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@colesbury colesbury deleted the gh-119241-capi-free-threaded-howto branch June 18, 2024 13:49
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 18, 2024
…GH-119877)

Some sections adapted from https://github.com/Quansight-Labs/free-threaded-compatibility/
written by Nathan Goldbaum.

(cherry picked from commit 02b272b)

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Nathan Goldbaum <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Jun 18, 2024

GH-120693 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 18, 2024
colesbury added a commit that referenced this pull request Jun 18, 2024
…9877) (#120693)

Some sections adapted from https://github.com/Quansight-Labs/free-threaded-compatibility/
written by Nathan Goldbaum.

(cherry picked from commit 02b272b)

Co-authored-by: Sam Gross <[email protected]>
Co-authored-by: Nathan Goldbaum <[email protected]>
@encukou
Copy link
Member

encukou commented Jun 18, 2024

Note that the HOWTO index was just reorganized (GH-119366); this HOWTO is currently missing from the rendered table of contents. Fix is in GH-120703.

picnixz pushed a commit to picnixz/cpython that referenced this pull request Jun 19, 2024
encukou pushed a commit that referenced this pull request Jun 20, 2024
…3 logical sections (GH-119366, GH-120703) (GH-120646)

Update the documentation howto index page and group docs into 3 logical sections (GH-119366)

(cherry picked from commit a26d27e)

Includes a follow-up fix to properly merge GH-119877:
* Add a link to free-threading HOWTO to the index (GH-120703)
  (cherry picked from commit 45d5cab)

Co-authored-by: Carol Willing <[email protected]>
Co-authored-by: Ned Batchelder <[email protected]>
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news topic-free-threading
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants