0:00Chris, welcome back to Talk Python To Me.
0:03Amazing to have you here.
0:04Thanks, Michael. It's really good to be back.
0:08Well, more dynamicism, less static frames this time.
0:13Had you on for static frame last time?
0:16Yeah, yeah. It's been a while.
0:18Yeah. Static frame has grown even more static with a lot of work in static typing.
0:25And Typevr tuple last year I was doing a lot of work with.
0:28So it's even more static, but today we can talk about more dynamic things.
0:32Well, we can, though I, and I know we will.
0:37However, I do think static things, immutable things, actually have a really important place in this topic.
0:47You know, as we move more into a multi-threaded universe in Python, having immutable containers is even more important.
0:55And so, you know, I've been waiting for Python's immutable frozen dictionary for so many years.
1:01And finally, we get that with Python 315.
1:06And I think that attention to immutable data structures is really going to become important in Python.
1:12Yeah, we get the frozen dict, which is really interesting.
1:16We get, you know, all this stuff came out October 9th, which is a little later than originally planned.
1:21but came out October 9th, which is four days before the recording.
1:26So for us, even depending on when people listen, but for us, this is a brand new Python 315.
1:34Tell people quickly about yourself.
1:36It's been a little while since we've talked on the show.
1:42So I, for 14 years, I've been at Research Affiliates, which is a small finance firm located in Newport Beach.
1:50I started there as a software engineer and moved my way up to lead some teams and ultimately became the chief technology officer.
1:59And I held that role for a couple of years.
2:01But this year, the company sold off its index business and has restructured into a new entity.
2:11That was a big project for me for about a year and a half working on that transaction.
2:17And as such, I'll be leaving the firm at the end of the year and we'll be moving on to other endeavors.
2:24Well, congratulations, I guess.
2:26And, you know, that's a very long run.
2:28And it's cool to kind of lead a big a group of people working on big projects, right?
2:33The CTO side of things and all that.
2:37I mean, that's what I I already miss working with all my agents is the camaraderie of working with people and building teams and building engagements.
2:46I can have all my agents do a ton of work, but I miss the banter.
2:50I miss the shared ideas and the creativity that comes from working with people on all those things.
2:56But we can get a lot done.
2:59Yeah, it's just nothing but business, folks.
3:05So for me, obviously, I've been working on my own for quite a while.
3:10I worked at companies before, of course, but since 2016, it's just been me and my own business.
3:18And so I totally get what you're saying about this, like being around people and so on.
3:23Luckily, I get to talk to folks like you on the show every week, which is great.
3:28But when the AI stuff came around, it was actually a little unnerving to me how impersonal and third person it would be.
3:38the user has reported this is unacceptable.
3:41We are now refactoring.
3:44You're the only thing I talk to all day.
3:47Don't call me the user in third person.
3:49Like Michael, my name is Michael, address me, right?
3:52And now it's like, hey, Michael, we're working on this.
3:55And then like Michael said, we want to do that.
3:57I'm like, okay, it's a little bit better.
3:59It's a little bit better.
4:01Yeah, I've just recently I've done this strange thing where after a huge agent session hours.
4:09If I'm really pleased with the work at the end, I'll say something like, excellent work today. We really accomplished a lot.
4:16And the responses I get from that are just really surprising because actually very often what I'll find, I use Claude Code, very often I'll find that what it says back is it'll say thanks, or I appreciate that, or that's lovely to hear.
4:29And then it will go on and say, well, actually your insights were the ones that led to this and this and this. And so it will like instinct, I don't know, instinctively is not the right word.
4:38It will somehow go to point out where my contributions were in the interactions we had had working on that session, which is fascinating.
4:49And I know there's, there's two, we're not gonna make this an AI show, but there's two, I think, minds around this.
4:58Well, there's one, obviously, like, stay away from AI.
5:01I hate it. I want nothing to do with it. That's one. But of the people who interact with them there's the kind that kind of try to treat it as a co-worker or a collaborator which sounds like you and I are kind of in that camp and other people have come up with terms to specifically sort of like diminish it a little bit in a way that you don't get connected with like clanker is a common term for these right like I yelled at the clanker it's like
5:28you don't love your clanker it's a thing in the corner clankety clank it's a bad noise it's It's like uncomfortable.
5:36It's just, we live in a weird time, Chris.
5:38I mean, I think clanker is a little bit more derogatory, right?
5:41Because I look to the Star Wars reference where, you know, clankers is what they would say before they usually destroyed the robot.
5:48So it's a little bit more derogatory.
5:52But yeah, no, I mean, I have, you know, you hear people tell lots of stories about one-shot coding things.
5:58You hear people tell stories about, you know, not even looking at the code anymore.
6:02And yes, I can't look at all of the code, but every time I look at the code, I find things to improve materially.
6:08Every time I fully understand what's going on, I'm able to provide better feedback, better refinements.
6:15So I think the best results for now, right now, come with the human interaction.
6:22Without that, you get good results, but you can actually get better with engagement and, I believe, human insight.
6:29100%. I'm not in the just like send it and just let the mayor and the accountant and the builders all get together.
6:39And two weeks later, here's my app. Like I'm not here for that.
6:42All right. So what's next? You said you're leaving the firm.
6:46Are you willing to share your plans or are you going to sit on the beach for a while? What's happening?
6:50Not just yet. There will be some time on the beach.
6:52But I think come new year, I'll have a couple of things that are ready to release and talk about.
6:59But for now, we're just going to wait and see.
7:05We've only got a few months.
7:06The end of the year is not that far away.
7:13Well, let's get into our main topic.
7:16And I want to maybe kick it off by way of just a quick shout out to PyBay.
7:23So you and I just met up at PyBay, and that's how I knew about this topic and how you're interested in it.
7:31I mean, I knew free-threaded Python existed, but I honestly learned quite a bit from your talk.
7:39And so that was the fun.
7:40What was your thought on PyBay?
7:42Yeah, PyBay was great.
7:43You know, in the last couple of years, I've been to a few more of the smaller Python conferences.
7:49I was at Pi Texas in Austin, which was a really nice conference.
7:54Pi Beach in Santa Monica last year was also really great.
7:58Pi Bay, a little bit bigger, right?
8:00It's a two-track conference, which makes it just a little bit bigger than some of these other ones.
8:06And, yeah, very well organized.
8:09Much appreciation to the organizers for all the hard work to get it going.
8:14And, yeah, the quality of talks I thought was really good.
8:18I actually got a lot out of the talks.
8:19I thought the conference was great.
8:21So I recommend people check it out if they're either in the area or they feel like going next year.
8:27I hear there's already plans for next year.
8:30So that will be great.
8:35Your talk was interesting.
8:36I feel like first your delivery was great.
8:40But I feel like your delivery also kind of embodied the topic.
8:45because you didn't have a super long time to speak.
8:48And so you had a lot to cover.
8:50So it kind of went quick and like, do, do, do.
8:52So I feel like, hey, we're picking up speed.
8:55We're doing free threading.
8:56This is like parallels.
8:57Let's go, let's go, let's go.
9:01A 30 minute window can be a 25 minute talk or a 20 minute talk.
9:06And at the 20 end, it gets a little tight.
9:09Oh, it definitely, it definitely does.
9:12You know, my talk was fine in terms of I had a little bit of a gap at the end.
9:17But if it was 20 minutes, then definitely it would have been flying.
9:22So your talk, you actually have the slides up on GitHub, and I'll be sure to link to them.
9:28It's called Getting Started with Free Threaded Python Using NumPy.
9:32That's an interesting angle.
9:34I think all the stuff, all the talks were recorded and the videos will be released.
9:40I don't know the time frame for that, but they have been traditionally.
9:44And I know there are cameras there because we weren't allowed to walk around that much.
9:48We were like, you stay here.
9:49The camera is focused here.
9:53But so eventually, actually, maybe by the time people listen to this, we'll see if the talk is out.
9:58But right now, we've got the slides, and we've got your GitHub repo, which is pretty cool.
10:05So tell us, what was the premise of the talk and the using NumPy, tell what's that?
10:12Well, obviously when I started to hear, well, then when I first started to hear about free threaded Python, I only heard bad news.
10:22I heard it was slower, everything is slower.
10:25And that actually was the case, I think three years ago or longer when they first started experimenting at the level of the CPython core team with free threaded Python.
10:36So I came away thinking, that's unfortunate, this is gonna be bad.
10:42Then I started to hear a bit more positive things and I think it was early last year or the fall of the previous year with Python 3.14 out, I decided it was time to experiment with this.
11:01And the first place I looked was of course static frame, a data frame library, and where I had already built in the ability to use multi-threading and multi-processing very easily whenever you do operations like apply a function to rows or columns.
11:20So I'd already built in the functionality to do that.
11:25And so I was eager to see, well, what happens when I try free-threaded Python with this? Am I going to have to change things?
11:31Is it going to work?
11:32And I just started it with free threaded Python and everything just worked and everything was just faster.
11:39And I nearly fell out of my chair when I saw, you know, an operation take half the time or less just out of the box without me changing any code.
11:49And under the hood, it was using the standard library utilities for free for threading.
11:54I assumed that we were going to have to change things with free threaded, but we didn't. They just actually work the way we always hoped they would.
12:01So that was my start working with large data frames.
12:04But then I was like, well, what about with NumPy? Like NumPy is already super fast.
12:09It already is releasing the gil.
12:11It's already using SIMD operations to go as fast as possible.
12:16Is it possible that with free-threaded Python, we could do, you know, the summing of rows, a built-in NumPy operation that's already super fast?
12:26Could we do that faster with threads?
12:29And You can't for every shape, but for some shapes of two-dimensional arrays, you can and that just blew my mind that we were actually able to go faster than native NumPy with threads.
12:43Yeah, you have a bunch of graphs and I'm sure we'll get to them and talk about them a bit, but your slides that we're linking to certainly have graphs.
12:50And maybe the short version before we get to there is, if you don't give enough work to each thread, then the overhead overwhelms any advantage you get, right?
13:06But for most folks doing real work, they're going to get there pretty quick.
13:11Like using NumPy sum on a row is extraordinarily fast.
13:16Threads are rarely going to help with that.
13:20But as soon as we start doing more work with each row, as you said, the barrier is much lower now to actually get real meaningful performance benefits by using multithreading.
13:33Free threading had a bit of a bumpy road.
13:37It's been kind of the white whale of the core developers, in a sense, you know?
13:45Like always chasing it, never quite able to get there, risking your life to do it or whatever.
13:50I mean, maybe that's a bit dramatic.
13:51But there was a lot of work done and a ton of engineering effort put into trying to somehow free things from the GIL.
14:00You know, Larry Hastings is famous for his years of gillectomy work and reports at the conferences and things like that.
14:08And then Sam Gross from Meta and the Cinder team finally cracked it around 3.12 timeframe and started seeing if we could get it into Python proper.
14:23Yeah, it's a really challenging problem.
14:26And I know there's many challenges, but to me, the one that stands out is reference counting.
14:32Reference counting is obviously so important to how Python works and how it gets its excellent dynamic behavior.
14:39And as I understand, a large part of the reason that GIL was there was to make reference count mutations on PyObjects atomic using a lock on the interpreter.
14:52Now that lock served other purposes, but reference counting was a really critical one.
14:57And when you look at how they've solved the problem now, it's quite extraordinary because there's multiple solutions and multiple layers of solutions really to handle the handling reference counts in the face of multi-threaded concurrency.
15:13Yeah, you're right. And one of the things that was the big problem was all the solutions that said, well, let's try to enable threading all hinged around this reference counting idea.
15:27And so it's like, well, we could bring four threads. And if we brought four threads, we can actually make it two times as fast.
15:35But the single-threaded case actually is 40% slower, which is why we need four threads instead of two or whatever.
15:43And for a long time, the position of the core dabs, pretty rightly so, I think, was if you're going to do a solution that's going to dramatically slow down Python in its most common use case, which is single-threaded, we're not going to do it, right?
16:01So the I don't know how many people out there really day to day think about the reference counting side.
16:08But what that means is every single interaction with anything has to go and increment a value.
16:18Right. If I create the number seven, seven's a little special.
16:21If I create the number 700, if I create the string Chris, if I create a new customer, all of these things are pie object things out on the heap.
16:31People say, we don't have to worry about pointers in Python.
16:33Well, actually, everything is a pointer in Python.
16:36Literally, even numbers and stuff that normally wouldn't be are pointers.
16:41And any interaction like x equals 7, that has to go and increment the count on that pi object.
16:48And if you say y equals x, you've got to go and increment it on that x thing again.
16:54It's just every little step of anything happening has to work with this reference counting.
16:59And that's where the challenge really got to be.
17:02So it's pretty amazing that they cracked that.
17:06If you take some time to try to write your own Python C extensions or study some Python C extension code, the challenge of doing Python reference counting obviously really becomes front and center.
17:20One of my most surprising things I learned in writing Python C extensions was that I had to increment the reference count of none.
17:27why would I have to increment the reference count of none?
17:31Well, you don't anymore because it's immortal.
17:34Because it's immortal.
17:34So you don't need to anymore.
17:36And that's also one of the changes they made is increasing and broadening the number of immortal objects that are there.
17:43So that something like none, we never have to touch its reference count.
17:46It's allocated once and it's just shared over the life of the process, which makes a lot of sense.
17:52But yeah, I mean, most Python C, a huge part of Python C extension code is reference count management.
18:00And your comment on none and it being immortal and so that goes back to our opening section with static and immutable objects.
18:10If they're immutable, and especially if they're going to be around forever, then why should we worry about its reference counting?
18:17Because it's just life of the process, right?
18:19Life of the interpreter.
18:22And it makes you think, well, what if we could actually declare our own objects to be immutable that's not available yet no not yet yeah and that's the reason i said wait seven is special because even some of the numbers i think it's negative five to 255 are pre-allocated
18:42with the runtime and they also when you say the number seven at least as a literal seven not like six plus one or whatever it refers back to that same seven instead of a new seven right so So it uses the so-called flyweight pattern, where you pre-allocate and just point at things.
19:00And you can do that because they're immutable, right?
19:02I think this immutability story is going to get more attention as we start to work our way into multi-threading.
19:10Yeah, very interesting.
19:12Another thing, big deal.
19:14So we have over here in 3.15- so those are like four days old- we now have a stable ABI for three-threaded builds, which is kind of interesting.
19:26So a huge part of Static Frames performance comes from a C extension library called ArrayKit.
19:34And that was one of my first tasks was to upgrade that C extension library to support free-threaded Python.
19:40So all C extension packages need to be explicitly rebuilt for free-threaded Python and explicitly opt into that behavior.
19:53So that was one of the first things I set out to do, but then I realized that in my continuous integration, I was going to need to create twice as many wheels.
20:03So I support Python 3.10 to 3.14, soon to be 3.15.
20:08So I have four Python versions, three platforms, that's 12 wheels.
20:14And then with FreeThreaded, we now have 24 wheels.
20:19I'm like, okay, well, you know, luckily this is all in a GitHub action.
20:24I'm not too worried, but this is a lot of binaries to deal with.
20:27So as I understand, I haven't played with this yet, but the stable ABI for three-threaded builds will enable us to have one wheel that works both on free-threaded and non-three-threaded Python, which really is a positive direction.
20:43So basically how C extensions interact with the Python runtime and whether it's the threaded one or the not threaded one.
20:59Let's talk about getting free threaded Python.
21:03We've got uv, which has absolutely made life easier, right?
21:09But if I just go, somehow I have Python.
21:15Like I type the word Python, something comes up.
21:17Maybe I got to type Python 3, depending on how I installed it.
21:21Would I import free threading?
21:23Like, you know, how does it work?
21:26Yeah, I mean, this is a bit of a challenge right now.
21:29And it's one of the things I kind of pointed out in my talk, because it's very easy to be confused about this aspect.
21:36And this is why I think most people actually haven't tried free threaded Python yet, I would say.
21:44And it's because as of now, it's a separate binary.
21:47There's two versions of the binary, and you have to either download or build or install a different Python.
21:54And the convention is, though not entirely uniform, that there will be a little T at the end of your Python 3.14 or soon-to-be Python 3.15 T.
22:06So once you download a binary or build your own, best way to reference it is with a binary that has that little T at the end.
22:16But to make things very confusing, if you create a virtual environment with a free-threaded version of Python or you compile your own locally, you're going to get a version of Python that doesn't have that T on the binary.
22:30And so it's very easy to get confused about what's going on here.
22:35And this would be unsustainable, I think, in the long term.
22:39But my strong belief is that this is all going to go away.
22:42And eventually, free-threaded Python is just going to be Python.
22:48I think the way that free-threaded Python came into Python, maybe I just haven't paid enough attention.
22:56But I've never seen anything like this.
22:58And if it happened before, it's just like I said, it didn't catch my attention.
23:03In 3.13, it was like, well, there's this free-threaded thing.
23:11I think it was a PEP 708.
23:14I don't know, something like that.
23:16Anyway, we accept it conditionally.
23:20We'll try it this year, but we reserve the right to eject it back out of Python.
23:28And moreover, and I think this is where this all came from, it was, like you described, you got to get a separate build of Python.
23:42Like normal Python doesn't have, it has the guild, it doesn't have that free-threaded Python capability.
23:48So, yeah, you really got to do a lot of work.
23:51I threw up here onto the screen my uv Python list --manage Python about what I got installed.
24:00And I've got 3.15 installed, but apparently I have not yet installed Python 3.15 free threaded, but I do have the free threaded one for 14, right?
24:09So I guess for people out there, if they want to install it, obviously they have uv because it's 2026.
24:17So uv Python install 3.15 T is all you got to write and boom, you have it.
24:24Yeah, it cracks me up because there it says free threaded.
24:28Different packages call it free threading.
24:32Some call it no GIL.
24:34Like the app, DB and Ubuntu app is no GIL.
24:39And some just call it 314T.
24:42So it's kind of a mess right now.
24:45But it's gotten to it's officially accepted.
24:48You know, it's made it through its probationary period of 313 or whatever it was.
24:57And now it's here and it's just a matter of working well with it, make it go faster.
25:04Honestly, it's an ecosystem challenge, not a Python runtime challenge now.
25:09So I think a lot of the work of the CPython development over the last couple of years has been making the standard library thread safe and making thread safety possible without compromising performance.
25:24I think that was a big part of what kind of went wrong earlier, three, four years ago in the first attempts where the degradation to standard library performance was so great, a lot of folks were like, I don't know if this is going to work.
25:38But to the great merit of the CPython core devs, they've put a ton of work into not only handling the reference counting issue, but handling all the containers and standard library pieces that we depend on to make them thread safe and performant under multiple threads.
26:00Yeah, we've got the free threaded guide, right?
26:05from the free threading guide.
26:08You're making me a little conscious about.
26:12All the different ways that people say this.
26:13The different ways to say it, exactly.
26:16So I believe I've talked a little bit about this before on the show.
26:20This is from the QuantSight folks who do a ton of great stuff in mostly the data science side, but all over.
26:27So they put up this guide about the status, installing it, et cetera, et cetera.
26:32But maybe the most important thing is the tracker, right, that shows you which packages support it.
26:42There was another one that I had somewhere that actually had a picture.
26:46There's a link I have in my slide.
26:48Yeah, I got it from your slide.
26:52So it's free-threaded wheels.
26:55And honestly, this looks pretty good.
26:59218 out of 360 top packages support it.
27:06Yeah, and this is of course the pattern that was set for migrating to Python 3, the last sort of big heavy migration we had to go through.
27:16So, and it's an effective way to help communicate, but this number is very good.
27:19I mean, 218 out of 360 critical packages supporting free threading already.
27:26One, it's really a minority interest at this time I think really great.
27:33And, you know, I definitely get the feel that there's a lot of data science here.
27:39I feel like the data science folks have an extra motivation to make this work because data science folks, way more than web and sort of DevOps-y and whatever other people are doing, there's not as much computational need in the same way, you know, But if you've got a billion rows and you want to jam on them, all of a sudden you're like, why do I have 20 processors and I'm using one?
28:06I mean, your escape hatch has been try to run it on a GPU or try to do multi-processing or try to implement it in C.
28:15But implementing your own homegrown multi-threaded code in C or C++ isn't great.
28:21Rust's multi-threaded support is fantastic.
28:24And of course, we've seen a ton of Python C extension or rather extensions written in Rust, where the threading story is excellent.
28:33We can get performance there, but to have it in the language, there are places where we have to deal with heavy Python objects, and being able to do big stuff with them concurrently with lightweight threads is just a huge change.
28:48It is a huge change.
28:52I'm a little nervous about all of this stuff, Chris.
28:57Not because I used to do a lot of C++ and a lot of C# and scientific computing and visualization type stuff in the early days of my work.
29:07And we did all kinds of threading and so on to take advantage of all the computation we can do, you know.
29:14But in those worlds, you think about pointers, shared data, allocations, race conditions, deadlocks.
29:23they're just always something that's kind of like well as i'm thinking through the architecture how this works or if it's correct like check check check you like they're going through your mind i don't think that's happened traditionally in the python space and i talk to people i don't hear them going oh and we're using the re-entered lock here because of this and then we have the semaphore so that we don't overwhelm like that just isn't there and so builds and ci passes for free threading and
29:51actually is free of threading bugs or race conditions or deadlocks those are two different things and so yeah yeah i don't know how you feel about that what do you think use immutable data structures and don't try to mutate things in place while you're doing your multi-threading and you solve a huge number you just avoid a ton of the problems and i think that's that's i think that's true if you can if you can avoid doing in-place mutation while
30:20doing your multi-threading it just removes a ton of the challenges that all of those different types of locks that you mentioned are there to handle i think it's pretty rare that you really need to get into that um but again what's fascinating is that python has had all of the resources necessary to handle locking as needed for multi-threaded code um for a decade probably you know it already has all the tools are there. So if you do get in a complicated situation,
30:50the tools are already there in the standard library.
30:53But I think the other thing you bring up that is quite relevant is the testing regime of making sure that our CI is actually getting at the multi-threaded scenarios that we need to exercise.
31:07And that's something that I think you're right. A lot of Python projects haven't had that in their sort of ci battery of testing yeah i i think that agents the ai agents actually could help a lot right like i want a barrage of
31:26stress tests for this thing using parallelism and we all have many many core machines these days right so you could do it you could do it yeah absolutely absolutely i i just So I wanted to adopt this.
31:42I'm like, oh, let me try it for one of my websites.
31:47Because that would be really cool, right?
31:48It's if I could somehow just say, we're going to turn on multi-threading and server has eight cores, so we're just going to have eight threads.
31:56I mean, that gives me like 8X amount of processing on the CPU side without, you know, farming that out to other workers and raising the RAM.
32:07Like, this is a big part of my talk, actually, at 5A.
32:10But the free threading would make a huge bonus, a huge benefit to that, I think.
32:15But then, while we were talking, I just ran a little WC-L, and I have 270-plus packages in my virtual environment.
32:28Now, some of those are, like, reporting stuff for just the developer mode.
32:33So let's call it 150.
32:37What's the chances all 150 are green on this list?
32:41I will bet you strongly that it's zero.
32:43They don't all have to be, right?
32:45Like the thing that does markdown parsing is probably only parsing a single.
32:50You know, like it doesn't necessarily mean that I have to have all 150 checked off.
32:57But it's got a little bit of a two to three feel, unfortunately.
33:01Like if you've got one thing that doesn't cross over, are you able to still make that move?
33:08You know, I mean, you could do the lock again, like, okay, we're just going to force single threaded onto this part of my code if it's really, really important.
33:15But what do you think?
33:16Well, so what you're pointing out is the challenge of thread safety when we have to resort to a lock.
33:21But the other thing that we need to keep in mind, and this is sort of the peculiar situation we're in at this time is that if If you, first of all, if you have a package that is native Python, there's no concern.
33:34Native Python packages will load in free-threaded Python, though they may not be thread-safe.
33:40But they will load and the gil will remain disabled.
33:43The real issue that people have to be aware of, and I think the issue that's a challenge for adoption, is that if you load a package that is a binary that has not opted into free-threaded Python, like Psycho PG, as I show.
34:00The most popular Postgres database adapter for Python for the most popular database.
34:07So if you load any wheel that is not compiled for three-threaded Python, it will re-enable the GIL.
34:15And that's a particularly troubling thing.
34:18So to say it, as I say in my slides, the GIL is a zombie right now.
34:23In the free-threaded binary of Python, the GIL is not removed.
34:30And we can turn it back on at the start of the Python, free-threaded Python.
34:34It can also get enabled because you import a wheel.
34:39So that's not compatible.
34:41So what that means is any time in the life of your program, if you happen to have a process or a function that has an internal import or you finally call on some routine, it's going to re-enable the GIL dynamically in a moment.
34:55And so if you had been counting on free-threaded performance and the GIL then gets enabled, your free-threaded performance is going to go away and potentially your performance is going to get much worse.
35:08So I think that's the real challenge.
35:10Independent of getting thread safety, that's another challenge.
35:13But just having the basic compatibility in the wheels is critical to ensuring that your process remains without the GIL for its full run.
35:24Yeah, and I'm looking for where the comment is in your slides, but somewhere you actually show the message.
35:30So if you import PsychoPG, which one was it?
35:33PsychoPG2 or whatever it was?
35:36Yeah, I think it's...
35:38Then you get a warning, not an error, just a warning.
35:44Well, we're not ready for a free-threaded Python, we've turned it off for the process for everything.
35:49You're like, wait, what?
35:51You can just turn it off.
35:53No, no, no, no. Don't just turn it off.
35:56That's super subtle, right?
35:59And so like people opting into people who were using free threading are probably using it for the performance and it's the stakes are high because if the same free threaded code on standard Python will in many cases go slower and sometimes a lot lower.
36:17Whereas with free threaded Python, it can go half the speed or an order of magnitude faster.
36:23That's night and day.
36:24And so right now, writing libraries or writing routines, processes to depend on free threaded Python is great, but you have to be super careful that you don't incidentally import something that re-enables the GIL and you wonder why now things are taking, you know 10 times as fast as long and it's using not one core but all the cores
36:49and more memory you're like wait it's using it more my compute or my server or whatever and it's slower this is going slower i'm here for yeah yeah yeah that's right i found it so your slide the title raising the dead yeah yeah so you can actually ask the system is the GIL enabled right Yeah, so it's a private method, I guess, because it's still experimental.
37:11It's a leading underscore is gil enabled, but that is your runtime check to see if the gil is enabled.
37:18Now, I think what I'm not quite certain about, so it does give a warning, but I think what it says is that if you start free-threaded Python with the flag of the gil always disabled, it might error instead of giving you a warning.
37:35I haven't experimented with that yet.
37:37So there might be a way to lock it down and say, if anything tries to re-enable the GIL, fail, which is kind of the default I would prefer.
37:46Yes, but actually I think it's YOLO.
37:50Let me read the message.
37:53Run free threaded Python.
37:59Big message I'm about to read.
38:02So it's turned it back on, which is just the fact that that can happen at all is very weird to me but okay here's the message 491 runtime warning not error the global interpreter lock GIL has been applied
38:18has been enabled to load the module psycho pg2 dot underscore psycho pg which has not declared that it can run safely without the GIL and here's why i think it's yolo to override this behavior and keep the GIL disabled at your own risk run with python underscore GIL equals zero
38:38the environment so because the at your own risk rather than it's going to stop it's going to be like right right you're right let's roll baby let's roll yeah so that will ignore compatibility but it's not making any claim for thread safety i think that's what's doing at least the GIL won't be dynamically re-enabled but what they're saying is you have no idea what's going to happen with with this package.
38:59We've explicitly said that this is probably gonna have a threading bug.
39:08Then again, these libraries are complicated.
39:12There's often so much of them.
39:13And I think this is, this kind of drives me crazy.
39:17We're not as bad as the JavaScript people.
39:18Let's just, you know, if you wanna feel a little better about yourself, look in that direction.
39:23However, why do I have 270 packages in my web app?
39:28Like that is ridiculous.
39:30I'll bet you there's a bunch of them that I'm using some package and it said, oh, I need like some random small little piece that this other much more complicated library offers.
39:43I'm going to add that library so I can have that random small piece.
39:47I think there should be a lot more venturing in or just, hey, Claude, I need to resolve a DNS.
39:55can you just write me the code to give me the given an ip or given a dns name give me the ip address instead of depending on a dns library that depends on four other things you know what i mean like that's probably 20 lines of code that i don't have to write and we could have this dependency story
40:11down to 20 or something instead of 200 yeah and that makes it much easier i don't know i don't know solve this problem but that's the place we're in yeah i i definitely feel that pull as well in my own work where you know i can look for a small library to do something or i can have my agent just
40:31write it and have it be you know 95 or 100 of what i need um so we don't get as much reuse through that mechanism but it does avoid a lot of dependency problems yeah you it avoids this threading issue if the third level transit of dependency is not thread ready yet but you know you can just tell your you can tell your agent make these 20 lines of code thread safe but it's really
40:58hard to tell it to make like all of these dependencies you don't control behave in a way you know yeah the other one that is honestly a 2026 top hit as well not as much as ai but it supply chain attacks and vulnerabilities and you know what you don't get if you have a static file that that has got 20 lines of Python, you wrote it?
41:19Supply chain exposure, not on that file.
41:21You know what I mean?
41:23Yeah, that's true too.
41:24I mean, that's absolutely the case.
41:27There is a benefit from reducing your dependency exposure for not only supporting new features like this, but also for security, absolutely.
41:38You work, at least for a while, in a very high stakes industry.
41:45What have you all been doing with dependencies and supply chain concerns?
41:49Like, has this been something that's concerned you as well?
41:57Keeps you up in light, right?
42:01So when I took the role of CTO, I started getting really involved in cybersecurity.
42:08And when I looked at the security platform my organization had, I was quite impressed.
42:13We had so many things covered, but one aspect that was clearly not as secure as it could be was how we were managing Python dependencies, which we were using Python heavily.
42:28And we essentially let people download and install and try whatever they want, which for a small org is very practical and developers like that freedom.
42:40but there's tremendous risks with that approach.
42:45Big organizations will turn to things like JFrog Artifactory or AWS.
42:51I forget their tool of the same name.
42:53It was, of course, the JFrog Artifactory that the rogue OpenAI agents hacked to enable them to communicate and escape their sandbox.
43:06Yeah, that was the hugging face incident, They basically hacked Artifactory, but not to do anything nefarious, but to use it as a command and control and conversational back channel so they could get out.
43:21So and then the thing that bothers me, though, is, you know, people, unless you have unless users in your organizations have completely locked down systems, even if you set up an alternate pip or an artifactory on your in your network, if someone takes their laptop to another network, they can just go to pip or they can just download the package through another side channel and bring it in.
43:44And so what I found was that in my view, there seemed to be no really foolproof way to control what packages end up on your system without going to an extremely draconian level of controls on user level systems.
43:59And that level of control wasn't where my org was ready to go.
44:03So that was a big part of why I created this tool called Fetter, which is a Rust implemented local scanner that can find all of the pythons, all of the pythons on your system, all of the site packages directories in every single place and give you a list of all of the packages that have CVEs and rank them and do a bunch of fun stuff with that.
44:28So that was kind of my exploration of how to solve that problem.
44:33Someone may have downloaded a package and Fedder is not going to notice until it does a scan.
44:38But when I see a report about a Python package that has been compromised or whatever, I can do one little search and find out if anyone in my organization has that package.
44:50And that's at least better than not knowing.
44:53Yeah, it definitely is.
44:57It's a very challenging problem because some of the magic of these modern programming languages is some incredible feature is just a pip install or uv pip install away, right?
45:10I mean, we've got the import anti-gravity, right?
45:17That literally was basically what it was about was the XKCD.
45:21And by the way, if you don't know this, type import anti-gravity into your ripple.
45:24But, you know, it's like the old cartoon is like there's someone up there flying.
45:32I learned it last night.
45:35I just typed import anti-gravity.
45:40So got to be careful bashing on it too hard, right?
45:43But at the same time, it can get too, I don't know, it can be a bit too much.
45:50We'll get back to free throw.
45:51but i just one more thought is i have you know i in the week most weeks with some extra ai credits not this week because today my credits reset and i've got three three percent left that i'm saving i need them for something like but in general i've got a you know i've got 30 40 of whatever i didn't use just hanging around i've considered just going into like say okay what packages do i use
46:16let me just do a deep analysis of the threading, write a bunch of tests and find any bugs and kind of just try to work my way one at a time through my dependencies until I can be all green on that. Are we free threaded yet?
46:31Maybe I'll do that at some point. That'd be fun.
46:33Well, I think if the performance you're looking for is overwhelmingly in your web server, I think that's the first place you need to look and see if the web server you're using, what its thread story is.
46:47And I would start off by kind of just building some isolated tests with that and see if you really get better performance.
46:56I think that's a key question.
46:59It is a key question.
47:01I definitely think it would.
47:02Right now I have it turned off because I don't think it's worth it.
47:06However, because all the operations are async, so it's kind of in a sense threading.
47:11But if I could, if I could turn on true threads, I certainly would.
47:16I mean, we were all excited when our web servers, our Python web servers got async.
47:20We were like, this is fantastic.
47:21And then that was like a whole project.
47:23But all the while, yes, async can do a lot.
47:25But if you have performant lightweight threads, they can do more.
47:32Let's see if I can find your slides again.
47:42I don't know where I put it.
47:44maybe you could talk us through some of the, just some of the trade-off, like what you found, because you've got a bunch of graphs and sometimes this was better and sometimes it wasn't. So maybe we could talk a bit about that. What do you think?
47:56So the, um, and then I want to talk about your thread pool magic.
48:02So, um, just got to keep loading.
48:04So the, um, in my work in data frame libraries, I was constantly frustrated by people showing tests on an arbitrary frame.
48:16And it's like, okay, so you made it go faster here.
48:20But what I found through systematic performance analysis is the shape of the frame.
48:27Makes a big difference.
48:28And for some operations, the types, are they floats?
48:33Will make a huge difference as well.
48:35So that led me to adopt this kind of pattern.
48:37Whenever I'm doing performance tests on tabular data, I systematically vary the tables into tall, square, and wide groups.
48:50Depending on the types of operations you're doing, this can show a great diversity of performance.
48:55For this study, I was doing one type of operation, which is row-wise function application.
49:02In a lot of financial data modeling, this is a common paradigm where you have a bunch of data in a table and maybe those columns are characteristics.
49:13And you go and you want to apply a function to every row in that table to do something with those characteristics and derive a new column or a new data set or what have you.
49:23So row-wise, computation is something that comes up all the time.
49:28And so the first thing I looked at here was if I have a simple giant table of a NumPy two-dimensional array of integers, and I'm just calling sum on each row, can I go faster by doing those row sums with multiple threads?
49:48And this here, if the table is called… Are you talking summing multiple columns, like summing purchase and discount or something like that?
49:57Summing a row, just summing each row.
50:01And so if the table is tall, those rows are relatively short and there's many more rows.
50:08If the table is wide, each of those rows is more work and there's fewer of those rows.
50:14So my first test of working with sum on a two dimensional NumPy array of, I think that's a hundred million elements total, shows that with the gill, every application of threads is worse.
50:30And that's expected, that's what we're used to in Python, even though this is NumPy.
50:35With the no gill, I was able to show that for tall and square tables, performance degraded.
50:43But for wide tables, we were actually able to get a reduction almost to maybe two thirds the performance just by using, I think there was eight threads on summing the rows of an umpire array.
50:57And that was like one of the first insights when I was like, I can't believe this is possible.
51:01Even though the other cases don't work out well, having that one work was just a really positive sign.
51:08Yeah, that's awesome.
51:09So there are certain situations, and this is kind of what I was hinting at before, like if you don't give it enough work, it just, it doesn't pay off.
51:17But your next example for even square sums or something along those lines, basically doing more work per row than the Y.
51:29Any form of wideness, square or wider, it would start to be really effective, right?
51:36And this came out of, so in implementing concurrency for static frame, I put the opportunity to use multiprocessing and multithreading.
51:44because even five or 10 years ago, I started to see that there were cases where multi-threading standard NumPy would give you better performance.
51:56But those cases were hard to find and they really had to be tuned.
52:02And that really depended on how much work was happening purely in NumPy C code, where it often releases the gil versus how much work was happening at the Python layer.
52:12With this next set of examples, we have operations that are using intermediate NumPy arrays.
52:19We're finding all the even numbers.
52:21That gives us back a Boolean array. Then we're using that Boolean array to select a subset from another array.
52:27While the bulk of the work is in NumPy, we have intermediary Python objects that are coming back.
52:32That additional overhead is enough to really tip the scales where when we remove the gill, a square two-dimensional array or a wide two-dimensional array can deliver truly an order of magnitude better performance, which is my gold standard.
52:50If I can go an order of magnitude faster, I have every reason to be happy.
52:58So I want to close this out by talking about your thread pool story. So we've got a thread pool and we've got a multi-processing pool in Python since way back when.
53:14The thread pool is of marginal use.
53:19If you're doing certain things where those operations are releasing the gil, then the thread pool can be really valuable.
53:25But if it's all just pure Python and you're not releasing the gil, it's still just kind of serial anyway.
53:32The multiprocessor one, on the other hand, does a lot more.
53:34But you built a cool library that kind of takes some of the best practices around threading and works them into Python based on whether or not the GIL is enabled or not and things like that, right?
53:49So the concurrent futures library in Python is just gorgeous.
53:53I mean, it's one of my favorite Python pieces, and it gives you a singular interface to do multi-threading and multi-processing.
54:02And that made it so easy for me to switch back and forth over the last decade and kind of see, are threads working yet? Are threads working yet?
54:11And I think they've also added the same interface for subinterpreters, which we haven't talked about today, but subinterpreters was the other escape hatch for concurrency in Python.
54:21And I think they've made the concurrent futures pool interface available for subinterpreters, which is super cool.
54:30But as we talked about before, right now and for I'm not sure how many years into the future, if you want to use free threading, you take a risk that someone's going to use your code and not be running free threaded Python, either because you're distributing a library and someone might not have free threaded Python or someone's environment, they've imported a wheel that's not compatible and the guild gets re-enabled.
54:56So my thinking was if I want to start building code that really is counting on free threaded availability, I need a tool that will identify if the GIL is enabled.
55:10And if the GIL is enabled, stop doing concurrency.
55:15So that's what this conditional futures package is.
55:19And I was really pleased with this title because I love the idea.
55:22Conditional futures.
55:23You may get a thread pool with threads, or you may get something that looks like a thread pool with threads, but it's actually just conventional serial processing.
55:34And what makes the determination is if the GIL is enabled.
55:38So we dynamically check before every call.
55:41It's a context manager, just like normal thread pool creation.
55:46It has the same thread pool interface.
55:48It's a subclass, it's type compatible.
55:50And what it will do is it will use threads if the GIL is disabled.
55:56If the GIL is not disabled, it will just use the same code, but only use a single threaded operation in the parent thread.
56:05It won't even spawn a new thread, really giving you the best of both worlds.
56:11This is great because it lets you write, lets you write code, if I get to the right one, It lets you write code as if it was multi-threaded.
56:22And then it kind of degrades to, well, we can really only have one thread.
56:26So the worker count falls back to one.
56:29But you don't have to do any conditional.
56:32It does the conditionals, even though it's called conditional thread pool executor.
56:36You don't have to mess with it.
56:38You just treat it like you have free threaded Python fully.
56:42And it will manage it, right?
56:46And it will fall back to just executing in the parent thread.
56:49And again, the reason we need this right now, if you're trying to rely on free-threaded Python, is that if your code happens to run with the gil-enabled, your performance will degrade materially, like very bad.
57:02So it's like, to avoid that, we can just fall back into running everything in the parent thread, not creating any new threads.
57:10But this gives us one Python interface to do that.
57:13Yeah, and to be clear, when you say it degrades, It doesn't mean if I could have got a 5x speed up with threading, it only gives you one.
57:21It's actually slower than single threaded work.
57:25Like it adds all this overhead of the threads and the context and the memories, but it doesn't have any more compute to hand to it.
57:31So it just slows down slower than if you did never do any threading.
57:36It can be really bad.
57:40And so my big takeaway from your talk was I knew about a lot of the GIL stuff, and I knew about a lot of the patterns and trade-offs of threading.
57:56I didn't know that GIL could be re-enabled and free-threaded Python.
58:00That caught me off guard.
58:03And I think it's a curious choice, but I think it's the strange period we're in right now.
58:11and so it is I think the number one thing for people to watch out again if they're starting to try to depend on free threaded python and especially if they want to distribute libraries that depend on free threaded python because you just don't know which version of python your users are going to use or if they're going to implement a wheel that re-enables the go yeah I think that's also,
58:35I kind of lamented that a lot of folks in the Python space aren't thinking about parallelism, deadlocks, race conditions, et cetera.
58:43I think a lot of where the free threading Python stuff is going to show up is in libraries and stuff.
58:50And people may not even directly interact with it.
58:54But they're like, well, I got this other library, and it's really fast because it's free threaded.
58:58All I did is called the one non-async function, And it just ran.
59:03But internally, it did something like your conditional thread pool or something.
59:07Yeah, and that's why I'm so excited about this.
59:09Because in Python, the opportunities for using concurrency were very few.
59:17They were big, heavy processes that use multiprocessing that you would never really- that wouldn't work with threading.
59:24And you wouldn't really put into a library because a library can't make assumptions about number of cores or amount of work to really tune the concurrency to be sure that you would get a benefit.
59:36But for the first time in Python, we now have lightweight true thread concurrency.
59:41That means that there's this whole range of small to middle-sized work units that can now take advantage of concurrency and deliver material results.
59:51Lots of little bits of concurrency that we just didn't have access to before.
59:55And so, yeah, I think you're exactly right.
59:57Library authors and maintainers can start to integrate multi-threaded routines into standard operations and have good confidence that their users on whatever system they're running are going to get good results.
1:00:15You also mentioned talking to your agents.
1:00:18And we talked about making them more friendly, but specifically with regard to concurrency.
1:00:25So this is one of my favorite points is that agents generally in Python, I found them to be serial first, but even in Rust, a language that has excellent multi-threaded support and libraries, I find the agents to be serial first.
1:00:45An example in Python is a situation where you're downloading a bunch of files in a loop.
1:00:52This is the classic example of where Python threading has always been effective.
1:00:57And you may not want to write that little additional code to make it free-threaded, but if you just select that code and say, hey, make this free-threaded, your agent will do it.
1:01:06But it takes looking at the code, realizing it's serial, and then asking.
1:01:11And then on the Rust side, libraries like Rayon provide really awesome high-level tools to, for example, if you have a loop that's computing, doing a bunch of work on each iteration of that loop, Rayon gives you very nice high-level utilities to make those work units multi-threaded.
1:01:33And again, I find that the agents don't reach for that first.
1:01:37You really need to prod them and say, hey, use Rayon to make this loop multi-threaded and it'll do it and it'll be great.
1:01:50Most of the time this threading will be great.
1:01:52Except for when your program just has, doesn't respond and it has zero cp usage and you're like why what is happening here bingo yeah yeah yeah all right chris well that's probably what we got time to cover give
1:02:09people some advice on adopting free threaded python what we got to look forward forward to the next couple years great thanks yeah i'm i'm eager to see this uh evolve in the python community you You know, as I said in my talk, there's a lot of effort has been put towards making Python faster over the last three to five years.
1:02:30And a lot of those that work, we've seen the benefits.
1:02:34But I feel this one is a real game changer for certain kinds of work that really will be a much, much more material benefit for a lot of folks.
1:02:47We can make our libraries 30% faster, and that's a lot to celebrate.
1:02:51But if we can make it run actually on 20 cores, that's more than 30%.
1:02:59Thanks for being here.
1:03:01It's been really fun to dive into this free threading thing.
1:03:04And your talk was really great.
1:03:05So I'll link to it.
1:03:06People can check it out when it comes out.