Hello, first post here ![]()
I’ve just released a first version of GitHub - dpdani/cereggii: Thread synchronization utilities for Python to PyPI, and I’m looking for feedback! ![]()
Looking at @colesbury’s nogil, and the work currently undergoing to port it to 3.13, I’ve been working on a sort of java.util.concurrent-like package for free-threaded Python (I’m referring to java here just because I know it better), and I’m currently working on making some atomic lock-free data structures, as part of my MSc thesis.
Let me explain why I think atomic lock-free data structures may be useful for Python.
Consider a program (or some phase of execution of a program) in which all threads are concurrently trying to modify a shared object.
For instance, consider an aggregation routine, where
- threads that are reading data, i.e. doing I/O;
- read the current value of the aggregation, i.e. read from a
dict; - compute the updated value; and
- try to update the aggregate, i.e. write to a
dict.
In this program, free-threaded CPython provides an improvement over non-free-threaded CPython only in so far as point (3) above is concerned.
Notice the behavior of blocking I/O in (1) does not change, compared to CPython with the GIL.
Points (2), and (4) instead provide no improvement under free-threaded CPython because the accesses around the dict itself require holding a lock, specific to the dict instance.
Compared to free-threaded CPython, the data types in cereggii provide some notable performance improvements. Consider for instance these examples: AtomicInt by dpdani · Pull Request #7 · dpdani/cereggii · GitHub
There are also other examples in the README, with more detailed explanations.
A question on packaging
When a user downloads my package cereggii, I’d like to tell that a free-threaded CPython is required to run it.
I currently do this by raising a warning at runtime if not getattr(sys.flags, "nogil", False) (and implicitly when the import raises a bunch of other warnings).
I’m currently basing my work on Sam’s original nogil implementation, and I’d like to eventually rebase on 3.13 when the first beta comes out.
My question is, when I do so, how can I express in my pyproject.toml that I specifically require a free-threaded interpreter?
Has this issue been discussed already?
P.S. I’m only distributing source right now (PyPI won’t allow the nogil39 tag), and I’m pretty sure this will not compile on Arm processors.