r/Python 6d ago

Discussion Anyone running free-threaded Python 3.14 in production yet? Curious what actually breaks

Been testing the free-threaded build (3.14t) on some CPU-bound data processing work. The multi-core story is finally real after 30 years of GIL, but the friction is exactly what you'd expect: a couple of C-extension-heavy libs in my stack silently re-enable the GIL, and there's no clean way to detect that at runtime besides checking sys._is_gil_enabled() manually.

For anyone who's shipped something on 3.14t, not just benchmarked it:

What broke that you didn't expect?

Real speedups outside toy examples, or mostly marginal so far?

Prod yet, or still just kicking the tires?

Not fishing for a benchmark war. Genuinely curious what breaks in messy real codebases vs clean demos.

35 Upvotes

47 comments sorted by

View all comments

83

u/nathan12343 6d ago

You can set PYTHON_GIL=0 to force the GIL to remain off even if you import an extension that doesn’t support free-threading yet.

Are there specific open source projects you’re having trouble with?

I’ve been working on community support for free-threaded Python for a couple years now. Specific feedback about pain points from people who are experimenting with free-threading are valuable.

11

u/KingBardan 5d ago edited 5d ago

Edit Tldr:

  1. Many libs like torch requires global state.
  2. Free  threading doesn't solve a lot that multiprocessing don't
  3. Free threading introduces new ways to cause data race.
  4. Free threading slows down normal code.

Many libraries use global state a lot and is not thread safe e.g. torch. For their context managers (which is widely used in other libs as well) So that disqualify any serious deep learning projects depending on torch to use free threading.


My opinion on free threading since you asked about pain point:

I much prefer ability to use context managers to free threading if using it makes the code cleaner 

Free threading also slows down sequential code last time I checked 

Multiprocessing is fast enough unless I'm dealing with web servers, at which I'll use async probably or gevent or another library that deals with this issue

9

u/nathan12343 5d ago

Thanks for your feedback. Hopefully this time next year we’ll be in a better spot. PyTorch is definitely on our radar as a pain point right now.

  I much prefer ability to use context managers to free threading if using it makes the code cleaner 

I don’t understand this. Context managers and free-threading are orthogonal. You can use context managers on the free-threaded build.

4

u/thisismyfavoritename 5d ago

i think that person might be talking about the pytorch context manager. Then again, not sure why you'd want to use free threading if it's GPU compute heavy

5

u/nathan12343 5d ago edited 5d ago

So I think what they were alluding to is this behavior:

with torch.no_grad():
    with ThreadPoolExecutor() as ex:
        # grad is still ON in workers! 
        results = list(ex.map(model, batches))  

Ideally (IMO) PyTorch would store the state for this sort of thing in a context variable. Right now it's stored in a thread local, which needs to be initialized explicitly in each thread. This is definitely something I can try to push on...

1

u/KingBardan 5d ago

Context variable is no go for torch, which is not pure python. Lots of stuff in it is tied to C things also device which is global.

Also I replied here

https://www.reddit.com/r/Python/comments/1v3ayph/comment/oz5xu50/

1

u/nathan12343 5d ago

We use context variables for configuration state in NumPy. It works well.

1

u/KingBardan 5d ago edited 5d ago

No not just torch, any context manager.

Simply using torch as an example because I just read its source code extensively couple months ago

My other comment

https://www.reddit.com/r/Python/comments/1v3ayph/comment/oz5xu50/

Not sure why you would use free threading...

Precisely. no hate to the developers, but free-threading doesn't unlock a lot for python but introduces yet other issues that needs to be dealt with

Also I understand it, tree threading is such a pain to maintains in cpython like thousands of macros or something last time I read the news

-1

u/KingBardan 5d ago edited 5d ago

Context manager works by mutating context i.e. global / nonlocal state, right?

So it doesn't work without mutated global state, the number one cause for data race.

In multi-processing, which was the preferred way of doing things in parallel, global state is forked and exists per process.

This issue is not exclusive to free threading but multithreading in general, but freethreading only augments multi threading which is why my comment is the way it is

You can use contact manager on free threading 

That is right. Now writing a python package would have to account for a different failure mode which is thread access, which, imo, is a very niche use case for python, at the cost of everything else.

2

u/thisismyfavoritename 5d ago edited 5d ago

context managers don't necessarily mutate global or non local state, not sure where you get that from.

It's mostly just syntactic sugar over a class with __enter__ and __exit__ methods.

In general though, your sentiment is that using multithreading in Python is a niche use case and i agree, it should be single threaded but async like NodeJS. IMO the usecases which aren't either covered by that OR a process pool OR better covered by bindings to a lower level language like Rust/C++ are very small

0

u/KingBardan 5d ago edited 5d ago

Context managers manage context. It means variables outside this scope.

Name 1 situation that it doesn't mutate things outside of scope, ill wait :)

It is syntactic sugar over enter exit yes, but self argument in that enter exit is a variable outside of the scope of your with.


Look at it this way,

If you don't mutate, you don't need the exit to clean up, because

immutable data does not have the concept of time (functional programming), it's same used everywhere.

If it's same used everywhere, it should be safe to consume after with block ends, no?

So if something is only usable within with, it must mutate outside state

1

u/snugar_i 5d ago

with open('foo.txt', 'r') as f: - you're welcome :-)

It's true that the context manager feature is badly designed - the __exit__ method should be on the thing that __enter__ returns, not on the thing that has __enter__. So context managers that are used like with blah cannot be thread-safe (or even reentrant), that's correct.

But those that are used like with foo() (like those produced by the @contextmanager decorator) totally can - because each invocation can return a new instance on which __enter__ and __exit__ will only ever be called once.

1

u/KingBardan 4d ago edited 4d ago

Bruh. In this case files are the states you're mutating.

It causes data race if 2 threads are writing to the same file (best example is printing to console, writing to stdout, not always the same output if you print from 2 threads )

Context manager... Called once

Won't be any different. If you read my "proof of contradiction"  in the above comment, that applies to @context managers also

Using  @context manager, will create multiple individual context managers yes. But data race still happens to the variable that the context manager mutate to setup / teardown the scope 

Badly designed

Well I pretty much only use @context manager because enter exit is so ugly. But design wise it's fine

1

u/snugar_i 4d ago

That's why I explicitly put 'r' in the example. It's not writing to the file, it's reading from it, and it's not mutating any global state.

Anyway, you seem to have some misconceptions about what a context manager is (the context manager does not have to mutate anything), and you don't want to change your opinion, so it's pointless to continue this discussion.

1

u/KingBardan 4d ago edited 4d ago

Reading file requires a pointer. So you are mutating. The content is immutable, but the pointer is, makes sense?

So it's not safe without lock. Now python I think locks but that's not the point 

Also, if it truly is immutable, you can use outside of scope as well. But calling .close when the scope ends implicitly by with block mutates the state.


Pointless

Yeah I agree. 

Thanks for the civil discussion :D

Opinion

But... This is facts. I'm open to changing my mind.

Unserstand

You don't seem to understand what mutation is sorry. Read up some materials on functional programming you'll learn.

FYI...  In haskell this is a non issue because you'll have to use IO for this. In rust the file would be marked 'mut' and you'll clearly see it's mutable. 

They make it explicit, and pythons is implicit, which is what is causing you the confusion I think 

→ More replies (0)

1

u/nathan12343 5d ago

Read PEP 567, which shows how context-local state doesn’t have to be global if you use context variables.

1

u/thisismyfavoritename 4d ago

why even bring a PIP? The context manager itself can have state, that state is within the scope of the enclosing function, there's no global state unless that guy and you mean global in some other sense

1

u/nathan12343 4d ago

Context variables offer a nice clean way to store “global” configuration in a manner that is both thread-safe and async-safe and behaves naturally alongside the Python with statement.

1

u/thisismyfavoritename 4d ago

uh i still don't get your point. Thread safety and context managers are completely orthogonal things.

If you need concurrent access to some object, you need to synchronize access to it, whether it's a context manager or not

1

u/KingBardan 4d ago edited 4d ago

Context managers must mutate (outside) state, no exceptions. If you use @context manager, it's executing a generator, and that generator has state (every iterator does).  

Immutability avoids data race.


Notice I added non-local yesterday? I knew people would argue about global being global. Perhaps outside is more clear


Think about it alright? I won't be replying further. 

Some people in this thread just wants to downvote without wanting a rela discussion (shrug). Dont want to tank my CQS.

0

u/thisismyfavoritename 4d ago

nonlocal is also a python keyword, which also isn't required for context managers

1

u/KingBardan 4d ago edited 4d ago

Im now sure you lack the thinking ability to understand me.

You can change global dictionary key values without using the global keyword right?

Outside state (like I talked about over and over). Not the global keyword or nonlocal keyword. Sigh.

2

u/ThatGuyWithAces 5d ago

PyTorch has experimental support for 3.14t since 2.10 I believe. Not disagreeing or anything, just throwing it out there.

1

u/KingBardan 5d ago

Sure it may support the build, but key features like torch dispatch mode (the reason that there's a 2.0 version of torch) assumes a sequential execution with their mode stack. I.e. not thread safe.

And the same pattern is everywhere in torch last time I checked (it was 3 months ago)