Latest / Talk Python To Me / #447: Parallel Python Apps with Sub Interpreters
Transcript
- 0:00It's an exciting time for the capabilities of Python.
- 0:02We have the Faster CPython initiative going strong,
- 0:05the recent async work, the adoption of typing,
- 0:09and on this episode, we discuss a new isolation
- 0:12and parallelization capability coming to Python
- 0:15through sub-interpreters.
- 0:16We have Eric Snow, who spearheaded the work
- 0:19to get them added to Python 3.12
- 0:21and is working on the Python API for them in 3.11,
- 0:24along with Anthony Shaw, who's been pushing the boundaries
- 0:27of what you can already do with sub-interpreters.
- 0:30This is Talk Python To Me, episode 446,
- 0:33recorded December 5th, 2023.
- 0:36Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:54This is your host, Michael Kennedy.
- 0:55Follow me on Mastodon, where I'm @mkennedy,
- 0:58and follow the podcast using @talkpython,
- 1:00both on fosstodon.org.
- 1:03Keep up with the show and listen to over seven years
- 1:05of past episodes at talkpython.fm.
- 1:08We've started streaming most of our episodes live on YouTube.
- 1:12Subscribe to our YouTube channel over at talkpython.fm
- 1:15slash YouTube to get notified about upcoming shows
- 1:18and be part of that episode.
- 1:19This episode is sponsored by PyBites Developer Mindset Program.
- 1:24PyBytes' core mission is to help you break the vicious cycle
- 1:27of tutorial paralysis through developing real-world applications.
- 1:30The PyBites Developer Mindset Program will help you build the confidence
- 1:34you need to become a highly effective developer.
- 1:37And it's brought to you by Sentry.
- 1:39Don't let those errors go unnoticed.
- 1:41Use Sentry.
- 1:42Get started at talkpython.fm/sentry.
- 1:45Anthony, Eric, hello, and welcome to Talk Python.
- 1:49Great.
- 1:50Hey, guys.
- 1:50It's really good to have you both here.
- 1:51You both have been on the show before, which is awesome.
- 1:55And Eric, we've talked about sub-interpreters before,
- 1:58but they were kind of a dream almost at the time.
- 2:01That's right.
- 2:02Now they feel pretty real.
- 2:04That's right.
- 2:05Yeah, it's been a long time coming.
- 2:07And I think the last time we talked, I've always been hopeful, but it seemed like it was getting
- 2:11closer.
- 2:12So with 312, we were able to land Per Interpreter Gill, which kind of was the last piece, the
- 2:18foundational part I wanted to do.
- 2:20A lot of cleanup, a lot of work that had to get done.
- 2:22But that last piece got in for 312.
- 2:25Excellent.
- 2:25Excellent.
- 2:26So good.
- 2:26And maybe let's just do a quick check-in with you all.
- 2:30It's been a while.
- 2:31You know, Anthony, start with you, I guess.
- 2:34Quick intro for people who don't know you, although I don't know how that's possible.
- 2:37And then just what you've been up to.
- 2:39Yeah.
- 2:39I'm Anthony Shaw.
- 2:41I work at Microsoft to lead the Python advocacy team.
- 2:44And I do lots of Python stuff, open source, testing things, building tools, blogging, building
- 2:54projects, sharing things.
- 2:55You have a book, something about the inside of Python?
- 2:58Oh, yeah, I have a book as well.
- 2:58Yeah?
- 2:58I forget about that.
- 2:59Yeah.
- 3:00Yeah, there's a book called CPython Internals, which is a book all about the Python compiler
- 3:04and how it works.
- 3:05And you suppress the memory of writing it, like it was too traumatic.
- 3:08It's down there.
- 3:09I keep forgetting.
- 3:11Yeah, that book was for 3.9.
- 3:15And people keep asking me if I'm going to update it for 3.13, maybe, because things keep changing.
- 3:22Yeah.
- 3:23Things are definitely changing at a more rapid pace than they were a few years ago as well.
- 3:27So that maybe makes it more challenging.
- 3:29Yeah, recently, I've been doing some more research as well.
- 3:34So I just finished my master's a few months ago and I started my PhD.
- 3:39And I'm looking at parallelism in Python as one of the topics.
- 3:43So I've been quite involved in subinterpreters and the free threading project and some other
- 3:49stuff as well.
- 3:50Awesome.
- 3:51Congratulations on the master's degree.
- 3:52That's really great.
- 3:53Thanks.
- 3:53And I didn't realize you were going further.
- 3:56So Eric.
- 3:56Eric Snow.
- 3:58So I've been working on Python as a core developer for over 10 years now.
- 4:03But I've been participating for even longer than that.
- 4:07And it's been good.
- 4:09I've worked on a variety of things, a lot of stuff down in the core runtime of CPython.
- 4:15And I've been working on this, trying to find a solution for multi-core Python since, really,
- 4:21since 2014.
- 4:23So I've been slowly, ever so slowly, working towards that goal.
- 4:28And we've made it with 3.12 and there's more work to do.
- 4:31But that's a lot of the stuff that I've been working on.
- 4:34I'm at Microsoft, but don't work with Anthony a whole lot.
- 4:39I work on the Python performance team with Guido and Grant Booger and Mark Shannon here
- 4:46at Kedrill.
- 4:47And we're just working generally to make Python faster.
- 4:50So my part of that has involved sub-interpreters.
- 4:53Awesome.
- 4:53Interestingly enough, it's only really this year that I've been able to work on all the sub-interpreter
- 5:00stuff full-time.
- 5:01Before that, I was working mostly on other stuff.
- 5:04So this year has been a good year for me.
- 5:07Yeah, I would say.
- 5:08That must be really exciting to get the, like, you know what?
- 5:11Why don't you just keep, just do that?
- 5:12That'd be awesome for us.
- 5:13Yeah, it's been awesome.
- 5:15Well, maybe since you're on the team, let's a quick check-in on FasterCPython.
- 5:20It's made a mega difference over the last couple of releases.
- 5:23Yeah, it's kind of interesting.
- 5:25Mark Shannon has a, definitely has a vision.
- 5:28He's developed a plan as of, like, years ago.
- 5:31But we finally were able to get him, put him in a position where he could do something
- 5:37about it.
- 5:37And we've all been kind of pitching in.
- 5:40A lot of it has to do with just applying some of the general ideas that are out there regarding
- 5:46dynamic languages and optimization.
- 5:47Things have been applied to other things like HHVM or various of the JavaScript runtimes.
- 5:55And so a lot of specialization, adaptive specialization, a few other techniques.
- 6:02But right now, so a lot of that stuff we're able to get in for 3.11.
- 6:09In 3.12, there wasn't quite as much impactful stuff.
- 6:13We're kind of gearing up to effectively add a JIT into CPython.
- 6:18And that's required a lot of kind of behind-the-scenes work to get things in the right places.
- 6:25So we're somewhat targeting 3.13 for that.
- 6:29So right now, I think with where things are at, we're kind of break-even performance-wise.
- 6:37But there's a lot of stuff that we can do.
- 6:39A lot of optimization work that really hasn't even been done yet that'll take that performance
- 6:45improvement up pretty drastically.
- 6:47It's kind of hard to say where we're going to be.
- 6:50But for 3.13, it's looking pretty good for at least some performance improvement because
- 6:57of the JITing and optimization work.
- 6:59That's exciting.
- 7:01Yeah, we have no real JIT at the moment, right?
- 7:03Not in CPython.
- 7:05Yeah.
- 7:05I mean, I know there's number.
- 7:07In Anthony, yeah, it is best.
- 7:08I know.
- 7:10Well, that's actually super exciting because I feel like that could be another big boost,
- 7:15potentially.
- 7:15You know, with the JIT, you come to all sorts of things like inlining of small methods and
- 7:20optimization based on type information and, yeah.
- 7:23Yep.
- 7:24All that stuff.
- 7:24One of the most exciting parts for me is that a lot of this work, not long after I joined
- 7:30the team, so two years ago, two and a half years ago, somewhere in there, pretty early
- 7:37on, we started reaching out to other folks, other projects that were interested in performance
- 7:43and performance of Python code.
- 7:45And we've worked pretty hard to collaborate with them.
- 7:49So like the team over at Meta, they have a lot of interest in making sure Python is very
- 7:57efficient.
- 7:57And so we've actually worked pretty closely with them and they're able to take advantage
- 8:03of a lot of the work that we've done, which is great.
- 8:05Yeah.
- 8:06There seems to be some synergy between the sender team and the faster CPython team.
- 8:10So awesome.
- 8:11But let's focus on a part that is there, but not really utilized very much yet, which is
- 8:19the sub-interpreter.
- 8:20So back on, when is this?
- 8:222019.
- 8:23Eric, I had you on and we talked about, can sub-interpreters free us from Python's gil?
- 8:29And then since then, this has been accepted, but it's Anthony's fault that we're here.
- 8:35Because Anthony posted over on Mastodon, hey, here's a new blog post, me running Python parallel
- 8:41applications with sub-interpreters.
- 8:42How about we use Flask and FastAPI and sub-interpreters and make that go fast?
- 8:48And that sounded more available in the Python level than I kind of realized the sub-interpreter
- 8:55stuff was.
- 8:56So that's super exciting, both of you.
- 8:58Yeah, it's been fun to play with it and try and build applications on it and stuff like
- 9:02that.
- 9:03And working with Eric probably over the last couple of months on things that we've discovered
- 9:08in that process, especially with C extensions.
- 9:12Daytime?
- 9:12Daytime?
- 9:13Yeah.
- 9:14Yeah, that's one.
- 9:16With C extensions.
- 9:18And I think that some of those challenges are going to be the same with free threading as
- 9:24well.
- 9:24So it's how C extensions have state, where they put it, whether that's thread safe.
- 9:30And as soon as you kind of open up the possibility of having multiple gills in one process, then
- 9:36what challenges does that create?
- 9:40Absolutely.
- 9:41Well, I guess maybe some nomenclature first.
- 9:43Not no-gil Python or sub-interpreter Python.
- 9:47Free-threaded.
- 9:48Is that what we're calling it?
- 9:49What's the name?
- 9:50How do we speak about this?
- 9:52It's not quite settled, but I think a lot of people have taken to referring to it as
- 9:58free-threaded.
- 9:58I can go with that.
- 9:59I mean, people still talk about no-gil, but free-threaded is probably the best bet.
- 10:04Are you describing what it does and why you care?
- 10:07Or are you describing the implementation, right?
- 10:09Like the implementation is it has no gill, so it can be free-threaded, or it has sub-interpreters,
- 10:13so it can be free-threaded.
- 10:15But really what you want is the free-threaded part.
- 10:17You don't care actually about the GIL too much, right?
- 10:19Kind of.
- 10:20Well, it's interesting.
- 10:21With sub-interpreters, it really isn't necessarily a free-threaded model.
- 10:26It's kind of free-threaded only in the part at which you're moving between interpreters.
- 10:33So you only have to care about it when you're interacting between interpreters.
- 10:37The rest of the time, you don't have to worry about it.
- 10:39Where with the no-gil is more kind of what we think of as free-threading, where everything
- 10:46is unsafe.
- 10:47And for people who don't know, the no-gil stuff is what's coming out of the sender team
- 10:50and from Sam Gross.
- 10:51And that was also approved, but with the biggest caveat I've ever seen on an approved PEP.
- 10:55Sure.
- 10:56Like, we approve this.
- 10:58We also reserve the right to completely undo it and not approve it anymore.
- 11:01But it's also a compiler flag that is an optional off-by-default situation.
- 11:06So it should be interesting.
- 11:08Yeah, we can maybe compare and contrast them a bit later as well.
- 11:11Yeah, absolutely.
- 11:12Well, let's start with what is an interpreter.
- 11:16So then how do we get to subinterpreters?
- 11:18And then what work did you have to do?
- 11:20I heard there was a few global variables that were being shared, Eric.
- 11:23Yeah.
- 11:24Maybe let's give people a quick rundown of what is this and how is this new feature in 3.12
- 11:30changing things?
- 11:31Yeah.
- 11:31Subinterpreters, in a Python process, when you run Python, everything that happens, all
- 11:39the machinery that's running your Python code is running with a certain amount of global state.
- 11:45And historically, you can think of it as, you know, across the whole process.
- 11:50You've got a bunch of global state.
- 11:52If you look at all the stuff like in the sys module, sys.modules or sys.whatever, all those
- 11:59things are shared across the whole runtime.
- 12:02So if you have different threads, for instance, running it, they all share that stuff, even though
- 12:07you're going to have different code running in each thread.
- 12:10So all of that runtime state is everything that Python needs in order to run.
- 12:15But what's interesting is that the vast majority of it, you can think of as the actual interpreter.
- 12:23And so that state, if we treat it as isolated and we're very careful about it, then we can
- 12:29have multiple of them.
- 12:30That means that when your Python code runs, that it can run with a different set of this
- 12:36global state, different modules imported, different things going on, different threads that are
- 12:42unrelated and really don't affect each other at all.
- 12:45And then with that in mind, you can take it one step farther and say, well, let's completely
- 12:51isolate those and not even have them share a gill, right?
- 12:55And then at that point, that's where the magic kind of happens.
- 12:59So that's kind of my first goal in this whole project was to get to that point.
- 13:05Because once you get there, then it opens up a lot of possibilities when it comes to
- 13:10concurrency and parallelism.
- 13:12Yeah.
- 13:12Then Anthony can start running with his blog posts and showing off good things.
- 13:16Yeah, absolutely.
- 13:17This portion of Talk Python To Me is brought to you by the Pybytes Python Developer Mindset
- 13:26Program.
- 13:26It's run by my two friends and frequent guests, Bob Belderbos and Julian Sequira.
- 13:32And instead of me telling you about it, let's hear them describe their program.
- 13:35In a world where AI, machine learning and large language models are revolutionizing how we live
- 13:43and work, Python stands at the forefront.
- 13:46Don't get left behind in this technological evolution.
- 13:51Tutorial paralysis?
- 13:52Tutorial paralysis?
- 13:52That's a thing of the past.
- 13:54With Pybytes Coaching, you move beyond endless tutorials to become an efficient, skilled Python
- 13:59developer.
- 14:00We focus on practical, real-world skills that prepare you for the future of tech.
- 14:05Join us at Pybytes and step into a world where Python isn't just a language, but a key to unlocking
- 14:12endless possibilities in the tech landscape.
- 14:15Check out our 12-week PDM program and embark on a journey to Python mastery.
- 14:20The future is Python.
- 14:22And with Pybytes, you're one step ahead.
- 14:24One thing I don't know the answer to, but might be interesting, is Python has a memory management
- 14:43story in front of the operating system, virtual memory that's assigned to the process with pools,
- 14:48arenas, blocks, those kinds of things.
- 14:51What's that look like with regard to sub-interpreters?
- 14:54Does each sub-interpreter have its own chunk or set of those for the memory it allocates,
- 14:59or is it still a shared one thing per process?
- 15:02It's per interpreter.
- 15:04This is something that was very global.
- 15:07And like you pointed out earlier, this whole project was all about taking all sorts of global
- 15:13state that was actually stored in C global variables all over the place, right?
- 15:18And pulling those in together into one place and moving those down from kind of the process
- 15:25global state down into each interpreter.
- 15:27So one of those things was all of the allocator state that we have for objects.
- 15:34And Python kind of has this idea of different levels of allocators.
- 15:39The object allocator is what's used heavily for Python objects, of course, but some other
- 15:44state as well.
- 15:45And the object allocator is the part that has all the arenas and everything, like you were
- 15:51saying.
- 15:51Yeah.
- 15:52So part of what I did before we could make the GIL per interpreter, we had to make the
- 15:58allocator state per interpreter.
- 15:59Well, the reason I think that it's interesting asking about it, one, because of the gill, obviously,
- 16:04but the other one is, it seems to me like these sub interpreters could be used for a little
- 16:09bit of stability or isolation or run some kind of code.
- 16:13And when that line exits, I want the memory freed.
- 16:16I want models unloaded.
- 16:18I wanted to go back to the way it was.
- 16:19You know what I mean?
- 16:21Whereas normally in Python, even if the memory becomes free, right, it's still got some of
- 16:25that, like, well, we allocated this stuff.
- 16:27Now we're holding it to refill it.
- 16:28And then, you know, you don't unimport modules and the modules can be pretty intense, actually,
- 16:34if, you know, if they start allocating a bunch of stuff themselves and so on.
- 16:37What do you guys think about this as an idea, as an aspect of it?
- 16:41Yeah, there's one example that's been coming across recently.
- 16:44And this is a pattern.
- 16:46I think it's a bit of an anti-pattern, actually.
- 16:48But some Python packages, they store, like, some state information in the module level.
- 16:56So an example is a SDK that I've been working with, which has just been rewritten to stop
- 17:02it from doing this.
- 17:03But you would put the API key of the SDK, you would import it.
- 17:08So you'd import X and then do, like, X dot API key equals.
- 17:13So it basically stores the API key in the module object, which is fine if you've got,
- 17:21you've imported the module once and you're using it once.
- 17:24But what you see is that if you put that in a web application, it just assumes that everyone
- 17:30uses the same key.
- 17:31So, you know, you can't have, you can't import that module and then connect to it with different
- 17:38API keys, like you'd have different users or something.
- 17:40Right.
- 17:40So you had some kind of multi-tenancy, right?
- 17:43Where, like, they would say, you know, enter their ChatGPT, OpenAI key, and then they could
- 17:49work on behalf of that, right?
- 17:50Like, potentially something like that, right?
- 17:52Yeah, exactly.
- 17:53So that's kind of like an API example.
- 17:55But there are other examples where, let's say you're loading data or something and it's,
- 17:59and it stores some temporary information somewhere in like a class attribute or even like a
- 18:05module attribute like that.
- 18:06If you've got one piece of code loading data and then in another thread in a web app or
- 18:13just in another thread generally, you're reading another piece of data and they're sharing state
- 18:18somehow and you've got no isolation.
- 18:20Some of that is due to the way that people have written the Python code or the extension
- 18:25code has kind of been built around, oh, we'll just put this information here.
- 18:31And they haven't really thought about the isolation.
- 18:33Sometimes it's because on the C level, especially that because the GIL was always there, they've
- 18:40never had to worry about it.
- 18:42So, you know, let's, you could just have a counter, for example, or there's an object, which is a
- 18:47dictionary that is a cache of something.
- 18:50and you just put that, as a static variable, and you just read and write from
- 18:55it.
- 18:56You've never had to worry about thread safety because the girl was there to kind of protect
- 18:59you.
- 19:00You probably shouldn't have built it that way, but it didn't really matter because it worked.
- 19:04what about this, Anthony?
- 19:06What if we can, if we can write it on one line, it'll probably be safe, right?
- 19:10If we can fit it just one line of Python code, it'd be okay.
- 19:13Yeah.
- 19:14Yeah.
- 19:16Dictionary.ad.
- 19:16What's wrong there?
- 19:17Dictionary.get.
- 19:18It's fine.
- 19:18Yeah.
- 19:19So yeah, what we're saying within sub interpreters, I think what's the concept that people will
- 19:25need to kind of understand is where the, where the isolation is, because there are different
- 19:31models for running parallel code.
- 19:32And at the moment we've got co-routines, which is asynchronous.
- 19:38So it can run concurrently.
- 19:40So that's, if you do async and a wait, or if you use the old, co-routine decorator,
- 19:44um, you've also got things like generators, which are kind of like a concurrent pattern.
- 19:49you've got threads that you can create.
- 19:52all of those live within the same interpreter and they share the same information.
- 19:58So you don't have to, if you create a thread inside that thread, you can read a variable
- 20:04from outside of that thread.
- 20:06and it doesn't complain.
- 20:08you don't need to create a lock at the moment.
- 20:10although in some situations you probably should.
- 20:13and you don't need to reimport modules and stuff like that, which, yeah, which can
- 20:19be fine.
- 20:20And then at the other extreme, you've got multi-processing, which is a module in the standard
- 20:25library that allows you to create extra Python processes and then kind of gives you like an
- 20:30API to talk to them and share information between them.
- 20:33And that's kind of like the app, the other extreme, which is you've got on it is the, you
- 20:38know, the ultimate level of isolation.
- 20:40You've got a whole separate Python process.
- 20:42but instead of interacting with it via the command line, you've kind of got this
- 20:46nicer API where you can almost treat it like it's in the same process as the one you're
- 20:52running from.
- 20:53Yeah.
- 20:53It's kind of magical actually that that at all where you get a return value from a process,
- 20:57for example.
- 20:58Right.
- 20:59Yeah.
- 20:59but the thing is, if you peel back the covers a little bit, then like how it
- 21:03sends information to the other Python process involves a lot of pickles.
- 21:08and it's not particularly efficient.
- 21:11And also a Python process has a lot of extra stuff that you maybe necessarily didn't even
- 21:17need.
- 21:18Like you get all this isolation from having it, but you have to import all the modules again.
- 21:22You have to create the arenas again, or the memory allocation.
- 21:26You have to do all the startup process again, which takes a lot of times, like at least 200
- 21:31million seconds.
- 21:31The Python code again, right?
- 21:32At least the PYC.
- 21:33Yeah.
- 21:34Yeah.
- 21:34Yeah, exactly.
- 21:35So you basically created like a whole separate Python.
- 21:38and if you do that just to run a small chunk of code, then it's not probably the best
- 21:45model at all.
- 21:45Yeah.
- 21:46You have a nice graph as well that shows like sort of the rate as you add more work and you
- 21:51need more parallelism.
- 21:52We'll get to that.
- 21:53I'm sure.
- 21:54you know, one thing that struck me coming to Python from other languages like C, C++,
- 21:59C#, there's very little locks and events, threading, coordinating stuff in Python.
- 22:06And I think that there's probably a ton of Python code that actually is not actually thread
- 22:12safe, but people kind of get away with it because the context switching is so coarse grained,
- 22:17right?
- 22:18Like you say, well, the gills there.
- 22:19So you only run one instruction at a time, but like this temporary invalid state you enter
- 22:24to as part of like, just your code running, like took money out of this account.
- 22:27And then I'm going to put it into that account.
- 22:29Those are multiple Python lines and there's nothing saying they couldn't get interrupted between
- 22:33one to the other.
- 22:34And then things are busted.
- 22:35Right.
- 22:36I feel there's, there's some concern about adding this concurrency, like, oh, we're having
- 22:40to worry about it.
- 22:41Like you probably should be worrying about it now.
- 22:42Not as much necessarily, but it's, I feel like people are getting away with it because
- 22:47it's so rare, but it's not, it's a non-zero possibility.
- 22:51What do you guys think?
- 22:51Yeah.
- 22:52I mean, those are real concerns.
- 22:53It's as there's been lots of discussion with the no-go work about really what, what matters,
- 23:01what we need to care about really what impact it's going to have.
- 23:05And I mean, it's probably going to have some impact on people with Python code, but it'll especially have impact on people that maintain extension modules.
- 23:15Yeah.
- 23:16But it really is all the pain that comes with free threading.
- 23:21that's what it introduces with the benefits as well, of course.
- 23:28But what's interesting, I'd like to think of sub interpreters kind of provide the same facility, but they force you to be explicit about what gets shared and they force you to do it in a thread safe way.
- 23:42So it's, you can't do it without thread safety.
- 23:46And so it's not an issue and it doesn't hurt that people really haven't used sub interpreters extensively up till now, whereas threads are kind of something that's been around for quite a while.
- 23:58Yeah, it has been in, well, it's sub interpreters have traditionally just been a thing you can do from C extensions or the C API, which really limits them from being used in just a standard.
- 24:09Like I'm working on my web app, so let's just throw in a couple of sub interpreters, you know, but in 313, is that when we're looking at having a Python level API for creating, interacting with?
- 24:20Yeah, I've been working on a PEP for that PEP 554 recently created a new PEP to replace that one, which is PEP 734.
- 24:30That's the one.
- 24:31So that's the one that I'm targeting for 313.
- 24:35And I don't know, it's pretty straightforward.
- 24:37Create interpreters and kind of look at them and with an interpreter, run some code, you know, pretty basic stuff.
- 24:46And then also because sub interpreters aren't quite so useful if you can't cooperate between them.
- 24:52But there's also a queue type that, you know, you push stuff on and you pop stuff off and just pretty basic.
- 25:00So you could write something like await queue.pop or something like that.
- 25:05Excellent.
- 25:06Yeah.
- 25:06So yeah, this is really cool.
- 25:08And the other thing that I wanted to talk about here, looks like you already have it in the pep, which is excellent.
- 25:13Somehow I missed that before.
- 25:14Is that there's an, we have thread pool executors, we have multi-processing pool executors, and this would be an interpreter pool executor.
- 25:23What's the thinking there?
- 25:25People are already familiar with using concurrent features.
- 25:28So if we can present the same API for sub interpreters, it makes it really easy because you can set it up with multi-processing or threads and switch it over to one of the other pool types without a lot of fuss.
- 25:41Right.
- 25:42Basically, with a clever import statement, you're good to go, right?
- 25:45From whatever import, like multi-processing pool executor as pool executor or interpreter pool executor as pool executor, and then the rest of the code could stay potentially.
- 25:54Yeah.
- 25:54What I expect-
- 25:55The communication, like you gotta, it's gotta kind of be a basic situation, right?
- 25:59Because there are assumptions.
- 26:01Yeah.
- 26:01Yeah.
- 26:01And it should work mostly the same way that you already use it with threads and multi-processing.
- 26:08But we'll see.
- 26:09There's some limitations with sub interpreters currently that I'm sure we'll work on solving as we can.
- 26:17So we'll see.
- 26:19It may not be quite as efficient as I'd like at first with the interpreter pool executor, because we'll probably end up doing some pickling stuff, kind of like multi-processing does.
- 26:29Although I expect it'll be a little more efficient.
- 26:31This portion of Talk Python To Me is brought to you by Sentry.
- 26:35You know Sentry for the air monitoring service, the one that we use right here at Talk Python.
- 26:39But this time, I want to tell you about a new and free workshop.
- 26:42Heaming the Kraken, managing a Python monorepo with Sentry.
- 26:47Join Salma Alam Nayour, senior developer advocate at Sentry, and David Winterbottom, head of engineering at Kraken Technologies, for an inside look into how he and his team develop
- 26:58deploy and maintain a rapidly evolving Python monorepo with over 4 million lines of code that powers the Kraken utility platform.
- 27:07In this workshop, David will share how his department of 500 developers, who deploy around 200 times a day, use Sentry to reduce noise, prioritize issues, and maintain code quality without relying on a dedicated Q&A team.
- 27:21You'll learn how to find and fix root causes of crashes, ways to prioritize the most urgent crashes and errors, and tips to streamline your workflow.
- 27:29Join them for free on Tuesday, February 27th, 2024 at 2 a.m. Pacific time.
- 27:35Just visit talkpython.fm/sentry dash monorepo.
- 27:39That link is in your podcast player show notes.
- 27:422 a.m. might be a little early here in the U.S., but go ahead and sign up anyway if you're a U.S. listener, because I'm sure they'll email you about a follow-up recording as well.
- 27:51Thank you to Sentry for supporting this episode.
- 27:55I was going to save this for later, but I think maybe it's worth talking about now.
- 27:58So first of all, Anthony, you wrote a lot about and have actually had some recent influence on what you can pass across, say, the starting code and then the running interpreter that's kind of like the sub-interpreter doing extra work.
- 28:10Want to talk about what data exchange there is?
- 28:13Yeah.
- 28:13So when you're using any of these models, multiprocessing, sub-interpreters, or threading, I guess you've got two or three things to worry about.
- 28:23One is how do you create it in the first place?
- 28:25So how do you create a process?
- 28:27How do you create an interpreter?
- 28:29How do you create a thread?
- 28:30The second thing is how do you send data to it?
- 28:33Because normally the reason you've created them is because you need it to do some work.
- 28:37So you've got the code, which is when you spawn it, when you create it.
- 28:42The code that you want it to run, but that code needs some sort of input, and that's probably going to be Python objects.
- 28:48It might be reading files, for example, or listening to a network socket.
- 28:53So it might be getting its input from somewhere else.
- 28:57But typically you need to give it parameters.
- 28:59Now, the way that works in multiprocessing is mostly reliant on pickle.
- 29:06If you start a process and you give it some data, either as a parameter or you create a queue and you send data down the queue or the pipe, for example, it pickles the data.
- 29:20So you can put a Python object in.
- 29:22It uses the pickle module.
- 29:24It converts it into a byte string and then it basically converts the byte string on the other end back into objects.
- 29:29That's got its limitations because not everything can be pickled.
- 29:35And also some objects, especially if you've got an object which has got objects in it and it's deeply nested or you've got a big complicated dictionary or something that's got all these strange types in it which can't necessarily be rehydrated just from a byte string.
- 29:53An alternative, actually, I do want to point out because for people who come across this issue quite a lot, there's another package called Dill on PyPI.
- 30:01So if you think of pickle, think of Dill.
- 30:05Dill is very similar to pickle.
- 30:08It has the same interface, but it can pickle slightly more exotic objects than pickle can.
- 30:14So often if you find that you've tried to pickle something, you try to share it with a process or a sub interpreter and it comes back and says, this can't be pickled.
- 30:24You can try Dill and see if that works.
- 30:27So, yeah, that's the typical way of doing it is that you would pickle an object and then on the other end, you would basically unpickle it back into another object.
- 30:38The downside of that is that it's pretty slow.
- 30:40It's equivalent.
- 30:41Like if you use the JSON module in Python, it's kind of similar, I guess, to converting something into JSON and then converting it from JSON back into a dictionary on the other end.
- 30:52Like it's not a super efficient way of doing it.
- 30:55So sub interpreters have another mechanism.
- 30:58I haven't read PEP 734 yet.
- 31:02So I don't know how much of this is in the new PEP, Eric, or if it's in the queue.
- 31:08But there's a...
- 31:09It's much the same.
- 31:11Okay, it's much the same.
- 31:12So there's another mechanism with sub interpreters.
- 31:15Because they share the same process, whereas multiprocessing doesn't, they're separate processes.
- 31:21Because they share the same process, you can basically put some data in a memory space, which can be read from a separate interpreter.
- 31:29Now you need to be...
- 31:31Well, Python needs to be really careful.
- 31:32You don't need to worry too much about it because that complexity is done for you.
- 31:37But there are certain types of objects that you can put in as parameters.
- 31:41You can send either as startup variables for your sub interpreter, or you can send via like a pipe, basically, backwards and forwards between the interpreters.
- 31:51And these are essentially all the immutable types for Python, which is like string, unicode strings, byte strings, bool, none, integer, float, and tuples.
- 32:04And you can do tuples of tuples as well.
- 32:07And it seems like the tuple part had to...
- 32:11Something that you added recently, right?
- 32:13It says, I implemented tuple sharing just last week.
- 32:15Yeah, that's in now.
- 32:17I really wanted to use it.
- 32:19So I thought, well, instead of keep...
- 32:21I kept complaining that it wasn't there.
- 32:23So I thought instead of complaining, I might as well talk to Eric and work out how to implement it.
- 32:28Yeah, that's awesome.
- 32:29But yeah, you can't share dictionaries.
- 32:31That's one thing.
- 32:31Yeah, exactly.
- 32:32So one thing that I thought that might be awesome, are you familiar with msgspec?
- 32:36You guys seen msgspec?
- 32:37It's like Pydantic in the sense that you create a class with types, but the parsing performance is quite a bit, like much, much faster.
- 32:4680 times faster than Pydantic, 10 times faster than Marshuro and Seatters and so on.
- 32:53And faster still even than, say, JSON or UJSON.
- 32:57So maybe it makes sense to use this, turn it into its serialization formats, bytes, send the bytes over and then pull it back.
- 33:04I don't know.
- 33:04It might give you a nice structured way.
- 33:06You can share bytes strings.
- 33:06So you can stick something into Pickle or you can use like msgspec or something like that to serialize something into a bytes string and then receive it on the other end and rehydrate it.
- 33:18Or even Pydantic.
- 33:19Like Pydantic is awesome as well.
- 33:20This is meant to be super fast with a little bit of less behavior, right?
- 33:24Yeah.
- 33:25So this is kind of a design thing.
- 33:26I think people need to consider when they're like, great, I can run everything in parallel now.
- 33:32But you have to kind of unwind and think about how you've designed your application.
- 33:36Like at which point do you fork off the work?
- 33:40Yeah.
- 33:40And how do you split the data?
- 33:43You can't just kind of go into it assuming, oh, we'll just have a pool of workers and we've kind of got this shared area of data that everybody just reads for.
- 33:53Yeah, I'll pass it a point or two of million entry list and I'll just run with it.
- 33:57Yeah, because I mean, in any language, you're going to get issues if you do that.
- 34:01Even if you've got shared memory and it's easier to read and write to the different spaces, you're going to get issues with locking.
- 34:07And I think it's also important with free threading.
- 34:10If you read the spec or kind of follow what's happening with free threading, it's not like the gills disappeared.
- 34:17The gills been replaced with other locks.
- 34:20Yeah.
- 34:21So there are still going to be locks.
- 34:23You can't just have no locks.
- 34:25Especially cross threads, right?
- 34:27Like it moves some of the reference counting stuff into like, well, it's fast on the default thread, the same thread.
- 34:34But if it goes to another, it has to kick in another more thread safe case that potentially is slower and so on.
- 34:39Yeah.
- 34:39So yeah, the really important thing with sub interpreters is that they have their own GIL.
- 34:45So each one has its own lock.
- 34:48So they can run fully in parallel just as they could with multi-processing.
- 34:52So I feel like a closer comparison with sub interpreters is multi-processing.
- 34:57Yeah, absolutely.
- 34:58Because they basically run fully in parallel.
- 35:00If you start four of them and you have four cores, each core is going to be busy doing work.
- 35:05You start them, you give them data, you can interact with them whilst they're running.
- 35:10And then when they're finished, they can close and they can be destroyed and cleaned up.
- 35:16So it's much closer to multi-processing.
- 35:20But the big difference is that the overhead both on the memory and CPU side of things is much smaller.
- 35:27Separate processes with multi-processing are pretty heavyweight.
- 35:31They're big workers.
- 35:33And then the other thing that's pretty significant is the time it takes to start one.
- 35:37So starting a process with multi-processing takes quite a lot of time.
- 35:42And it's significantly, I think it's like 20 or 30 times faster to start a sub interpreter.
- 35:48You have a bunch of graphs for it somewhere.
- 35:51There we go.
- 35:51Yeah.
- 35:52So I scrolled past it.
- 35:53There we go.
- 35:54It's not exactly the same, but kind of captures a lot of it there.
- 35:58So one thing that I think is exciting, Eric, is the interpreter pool, sub interpreter pool.
- 36:04Because a lot of the difference between the threading and the sub interpreter performance
- 36:09is that startup of the new arenas and like importing the standard library,
- 36:14all that kind of stuff that still is going to happen.
- 36:16But once those things are loaded up in the process, they could be handed work easily, right?
- 36:21And so if you've got a pool of, you know, like say that you have 10 cores,
- 36:24you've got 10 of them just chilling or however many, you know,
- 36:27you've sort of done enough work to like do in parallel,
- 36:29then you could have them laying around and just send like,
- 36:33okay, now I want you to run this function.
- 36:34And now I want you to run this.
- 36:35And that one means go call that API and then process it.
- 36:38I think you could get the difference between threading and sub interpreters
- 36:41a lot lower by having them kind of reuse basically.
- 36:44Yep.
- 36:45Absolutely.
- 36:46There's some of the key difference,
- 36:51I think is mostly that when you have mutable data,
- 36:54whereas with threads, you can share it.
- 36:57So threads can kind of talk to each other through the data that they share with each other.
- 37:02Whereas with sub interpreters, there are a lot of restrictions.
- 37:05And I expect we'll work on that to an extent,
- 37:08but it's also part of the programming model.
- 37:11And like Anthony was saying,
- 37:12if you really want to take advantage of parallelism,
- 37:15you need to think about it.
- 37:17You need to actually be careful about your data and how you're splitting up your work.
- 37:22I think there's going to be design patterns that we come to know,
- 37:25or conventions we come to know,
- 37:26like, let's suppose I need some calculation,
- 37:29and I'm going to use it in a for loop.
- 37:31You don't run the calculation if it's the same over and over.
- 37:34Every time through the loop, you run it,
- 37:35and then you use the result, right?
- 37:37So in this, you know, a similar thing here would be like,
- 37:39well, if you're going to process a bunch of data,
- 37:41and the data comes from, say, a database,
- 37:43don't do the query and hand it all the records.
- 37:46Just tell it, go get that data from the database.
- 37:48That way it's already serialized in the right process,
- 37:51and there's not this cross serialization to either pickling
- 37:55or whatever mechanism you come up with, right?
- 37:57But like, try to think about when you get the data,
- 38:00can you delay it until it's in the sub process,
- 38:02or sub interpreter rather, and so on, right?
- 38:05Yeah, definitely.
- 38:06One interesting thing is that pep734,
- 38:10I've included memory view as one of the types that's supported.
- 38:15So basically, you can take a memory view of any kind of object
- 38:20that implements the buffer protocol,
- 38:23so like numpy arrays and stuff like that,
- 38:26and pass that memory view through to another interpreter,
- 38:29and you can use it, and it doesn't make a copy or anything.
- 38:31It actually uses the same underlying data.
- 38:34They actually get shared.
- 38:36Oh, that's interesting.
- 38:37Yeah, and I think there's even more room for that
- 38:41with other types, but we're starting small.
- 38:45But the key thing there is that, like you're saying,
- 38:49I mean, with coming up with different models and patterns
- 38:54and libraries, I'm sure they'll come up
- 38:57as people feel out really what's the easiest way
- 39:01to take advantage of these features,
- 39:03and that's the sort of thing that will apply
- 39:05not just to general free-threaded, like, no-gil,
- 39:08but also sub-interpreters.
- 39:10Definitely.
- 39:11It's going to be exciting.
- 39:12So I guess I want to move on and talk about
- 39:15working with this in Python
- 39:16and the stuff that you've done, Anthony,
- 39:18but maybe a quick comment from the audience.
- 39:21Jazzy asks, is this built on top of a queue,
- 39:23which is built on top of a linked list?
- 39:24Because I'm building this, and my research led me
- 39:27to these data structures.
- 39:28I guess that's the communication across sub-interpreter,
- 39:32cross-interpreter communication.
- 39:34Yeah, with sub-interpreters, like in PEPS 734,
- 39:36a queue implements the same interfaces
- 39:40as the queue from the queue module.
- 39:42But there's no reason why people couldn't implement
- 39:46whatever data structure they want
- 39:48for communicating between sub-interpreters,
- 39:50and then that data structure's in charge
- 39:52of preserving thread safety and so forth.
- 39:55Yeah, excellent.
- 39:56Yeah, it's not a standard queue.
- 39:57It's like a concurrent queue
- 39:58or something along those lines.
- 40:00Yeah.
- 40:00Yeah.
- 40:01All right, so all of this we've been talking about here
- 40:04is we're looking at this cool interpreter pool
- 40:07executor stuff.
- 40:08That's in draft format, Anthony, for 3.13.
- 40:12And somehow I'm looking at this running Python parallel applications
- 40:16and sub-interpreters that you're running.
- 40:18What's going on here?
- 40:20How do you do this magic?
- 40:21You need to know the secret password.
- 40:23So in Python 3.12,
- 40:30the C API for creating sub-interpreters was included,
- 40:36and a lot of the mechanism
- 40:38for creating sub-interpreters was included.
- 40:42So there's also a...
- 40:44In CPython, there's a standard library,
- 40:47which I think everybody kind of knows.
- 40:49And then there are some hidden modules,
- 40:53which are mostly used for testing.
- 40:55So not all of them get bundled,
- 40:58I think, in the distribution.
- 41:00I think a lot of the test modules get taken out.
- 41:03But there are some hidden modules you can use for testing
- 41:06or because a lot of the test suite for CPython
- 41:09has to test C APIs,
- 41:11and nobody really wants to write unit tests in C.
- 41:14So they write the tests in Python,
- 41:16and then they kind of create these modules
- 41:19that basically just call the C functions.
- 41:20And so you can get the test coverage and do the testing from Python code.
- 41:24So I guess what was from PEP6...
- 41:29I can't remember.
- 41:30I look at how many PEP6...
- 41:32Eric will probably know.
- 41:34What is now PEP734,
- 41:36but the Python interface to create sub-interpreters,
- 41:42a version of that was included in 3.12.
- 41:45So you can import this module called underscore XX sub-interpreters.
- 41:49And it's called underscore XX
- 41:51because it kind of indicates that it's experimental
- 41:54and it's underscore because you probably shouldn't be using it.
- 41:57It's not safe for work to me.
- 41:59I mean, I don't know.
- 42:02But it provides a good way of people actually testing this stuff
- 42:08and seeing what happens if I import my C extension from a sub-interpreter.
- 42:13So that's kind of some of what I've been doing,
- 42:15is looking at, okay, what can we try and do in parallel?
- 42:20And this blog post, I wanted to try a WSGI or an ASGI web app.
- 42:28And the typical pattern that you have at the moment,
- 42:32and I guess how a lot of people would be using parallel code,
- 42:35but without really realizing it,
- 42:37is when you deploy a web app for Django Flask or FastAPI,
- 42:42you can't have one guild per web server
- 42:46because if you've got one guild per web server,
- 42:48you can only have one user per website,
- 42:51which is not great.
- 42:53So the way that most web servers implement this
- 42:57is that they have a pool of workers.
- 43:00GUnicorn does that by spawning Python processes
- 43:04and then using the multi-processing module.
- 43:06So it basically creates multiple Python processes all listening to the same socket.
- 43:11And then when a web request comes in, one of them takes that request.
- 43:16It also then inside that has a thread pool.
- 43:20So even basically a thread pool is better for concurrent code.
- 43:24So GUnicorn normally is used in a multi-worker, multi-thread model.
- 43:30That's how we kind of talk about it.
- 43:32So you'd have the number of workers that you have CPU cores,
- 43:35and then inside that you'd have multiple threads.
- 43:39So it kind of means you can handle more requests at a time.
- 43:43If you've got eight cores, you can handle at least eight requests at a time.
- 43:47However, because most web code can be concurrent on the backend,
- 43:52like you're making a database query,
- 43:54or you're reading some stuff from a file like that,
- 43:57that doesn't necessarily need to hold the GIL.
- 44:00So you can run it concurrently, which is why you have multiple threads.
- 44:04So even if you've only got eight CPU cores,
- 44:06you can actually handle 16 or 32 web requests at once,
- 44:12because some of them will be waiting for the database server to finish running at SQL query,
- 44:16or the API that it called to actually reply.
- 44:20So what I wanted to do with this experiment was to look at the multi-worker,
- 44:25multi-thread model for web apps,
- 44:27and say, okay, could the worker be a sub-interpreter?
- 44:32And like, what difference would that make?
- 44:35So instead of using multi-processing for the workers,
- 44:38could I use sub-interpreters for the workers?
- 44:40So even though the Python interface in 3.12 is experimental,
- 44:45I basically wanted to adapt Hypercorn,
- 44:48which is a web server for ASCII and WSGI apps in Python.
- 44:53I wanted to adapt Hypercorn
- 44:55and basically start Hypercorn workers from a sub-interpreter pool,
- 44:59and then seeing if I can run Django, Flask, and FastAPI in a sub-interpreter.
- 45:04So a single process, single Python process,
- 45:07but running across multiple cores and listening to web requests
- 45:11and basically running and serving web requests with multiple gills.
- 45:15So that was the task.
- 45:16In the article, you said you had started with Gunicorn,
- 45:19and they just made too many assumptions about the web workers being truly sub-processes.
- 45:26But Hypercorn was a better fit, you said.
- 45:28Yeah, it was easier to implement this experiment in Hypercorn.
- 45:32It had like a single entry point.
- 45:35Because when you start an interpreter, when you start a sub-interpreter,
- 45:39you need to import the modules that you want to use.
- 45:41You can't just say, run this function over here.
- 45:46You can, but if that function relies on something else that you've imported,
- 45:50you need to import that from the new sub-interpreter.
- 45:52So what I did with this experiment was basically start a sub-interpreter
- 45:58that imports Hypercorn, listens to the sockets,
- 46:01and then is ready to serve web requests.
- 46:03Interesting.
- 46:04Okay.
- 46:04And at a minimum, you got it working, right?
- 46:07Yeah, it did a hello world.
- 46:09So we got that working.
- 46:13So I was, yeah, pleased with that.
- 46:16And then kind of started doing some more testing of it.
- 46:19So, you know, how many concurrent requests can I make at once?
- 46:22How does it handle that?
- 46:23What does my CPU core load look like?
- 46:25Is it distributing it well?
- 46:27And then kind of some of the questions are, you know,
- 46:31how do you share data between the sub-interpreters?
- 46:35So the minimum I had to do was each sub-interpreter needs to know which web socket should I be listening to.
- 46:42So like which network socket, once I've started, what port is it running on?
- 46:46And is it running on multiple ports?
- 46:49And which one should I listen to?
- 46:50So yeah, that's the first thing I had to do.
- 46:52Nice.
- 46:52Yeah.
- 46:53Maybe just tell people real quick about just like what are the commands like at the Python level that you look at in order to create an interpreter, run some code on it and so on.
- 47:02What's this weird world look like?
- 47:04Derek, do you want to cover that?
- 47:06Yeah, there is a whole lot.
- 47:07I mean, if we talk about PEP 734, you have an interpreters module with a create function in it that returns you an interpreter object.
- 47:17And then once you have the interpreter object, it has a function called run or a method.
- 47:24The interpreter object also has a method called exec.
- 47:27I'm trying to remember.
- 47:29It was exec sync because it's synchronous with the current thread.
- 47:34And whereas exec run will create a new thread for you and run things in that there.
- 47:40So they're kind of different use cases.
- 47:41But it's basically the same thing.
- 47:43You have some code.
- 47:44Currently supports just you give it a string with all your code on it, like you load it from a file or something.
- 47:53You know, basically it's a script that's going to run in that sub integer.
- 47:56Alternately, you can give it a function.
- 48:00And as long as that function isn't a closure, doesn't have any arguments and stuff like that.
- 48:05So it's just like really basic, basically a script, right?
- 48:08If you got something like that, you can also pass that through.
- 48:12And then it runs it.
- 48:14And that's just about it.
- 48:16If you want to get some results back, you're going to have to manually pass them back kind of like you do with threads.
- 48:21But that's something people already understand pretty well.
- 48:24Right.
- 48:24And create one of those channels.
- 48:25And then you just wait for it to exit and then read from the channel or something like that.
- 48:29Yeah.
- 48:29Yeah.
- 48:30And so there's a way to say things like just run.
- 48:33And there's also a way to say create an interpreter.
- 48:36And then you could use the interpreter to do things.
- 48:38And that lets you only pay the process like startup cost once, right?
- 48:43Yeah.
- 48:44Yeah.
- 48:44And you can also, you can call that the run multiple times.
- 48:49And each time it kind of adds on to what ran before.
- 48:52So if you run some code that modifies things or import some modules and that sort of thing, those will still be there the next time you run some code in that interpreter.
- 49:02Which is nice because then if you've got some startup stuff that you need to do one time, you can do that ahead of time right after you create the interpreter.
- 49:10But then in kind of your loop in your worker, then you run again and all that stuff is ready to go.
- 49:16Oh, that's interesting because when I think about, say, my web apps, a lot of them talk to MongoDB and use Beanie.
- 49:22And you go to Beanie and you tell it to like create a connection or a MongoDB client pool.
- 49:28And it does all that stuff.
- 49:30And then you just ambiently talk to it.
- 49:31Like go to that, you know, kind of like Django or whatever, right?
- 49:33Go to that class and do a query on it.
- 49:35You could run that startup code like once potentially and have that pool just hanging around for subsequent work.
- 49:40Nice.
- 49:41All right.
- 49:42Let's see some more stuff.
- 49:45So you said you got it working pretty well, Anthony.
- 49:47And you said one of the challenges was trying to get it to shut down, right?
- 49:51Mm-hmm.
- 49:52Yeah.
- 49:52Yeah.
- 49:53So in Python, when you start a Python process, you can press Ctrl-C to quit, which is a keyboard interrupt.
- 50:01That kind of sends the interrupt in that process.
- 50:07All of these web servers have got like a mechanism for cleanly shutting down because you don't want to just, if you press Ctrl-C, you don't want to just terminate the processes.
- 50:16Because when you write an ASGI app in particular, you can have like events that you can do.
- 50:22So people who've done FastAPI probably know the on-event decorator that you can put and say, when my app starts up, create a database connection pool.
- 50:32And when it shuts down, then go and clean up all this stuff.
- 50:35So if the web servers decided to shut down for whatever reason, whether you've pressed Ctrl-C or it just decided to close for whatever reason, it needs to tell all the workers to shut down cleanly.
- 50:49So signals, like the signals module, doesn't work between sub-interpreters because it kind of sits in the interpreter state, from what I understand.
- 51:00So what I did was basically use a channel so that the main worker, like the coordinator, when that had a shutdown request, it would send a message to all of the sub-interpreters to say, okay, can you stop now?
- 51:15And then it would kick off a job, basically tell Hypercorn, in this case, to shut down cleanly, call any shutdown functions that you might have, and then log a message to say that it's shutting down as well.
- 51:28Because the other thing is, with web servers, if it just terminated immediately, and then you looked at your logs, and you were like, okay, why did the website suddenly stop working?
- 51:38And there was no log entries, and it just went from, I'm handling requests, to just, you know, absolute silence.
- 51:44Yes.
- 51:44That also wouldn't be very helpful.
- 51:46So it needs to write log messages, it needs to call, like, shutdown functions and stuff.
- 51:50Right.
- 51:51So what I did was, and this is, I guess, where it's kind of a bit of a turtles all the way down, but inside the sub-interpreter, I start another thread.
- 52:02Because if you have a poller which listens to a signal on a channel, that's a blocking operation.
- 52:09So, you know, at the bottom of my sub-interpreter code, I've got, okay, run Hypercorn.
- 52:15So it's going to run, it's going to listen to Socket's web requests.
- 52:18But I need to also be able to run concurrently in the sub-interpreter a loop which listens to the communication channel and sees if a shutdown request has been sent.
- 52:30So this is kind of a, maybe an implementation detail of how interpreters work in Python, but interpreters have threads as well.
- 52:39So you can start threads inside interpreters.
- 52:42So similar to what I said with GUnicorn and Hypercorn, how you've got multi-worker, multi-thread, like each worker has its own threads.
- 52:49In Python, interpreters have the threads.
- 52:53So you can start a sub-interpreter and then inside that sub-interpreter, you can also start multiple threads.
- 52:59And you can do coroutines and all that kind of stuff as well.
- 53:03So basically what I did is to start a sub-interpreter which also starts a thread,
- 53:08and that thread listens to the communication channel and then waits for a shutdown request.
- 53:12Right.
- 53:12Tells Hypercorn, all right, you're done.
- 53:16We're out of here.
- 53:17Yeah.
- 53:17Okay.
- 53:18Interesting.
- 53:18Here's an interesting question from the audience, from Chris.
- 53:21Well, it says, when you, we talked about the global kind of startup, like if you run that once, it'll already be set.
- 53:27And, you know, does that make code somewhat non-deterministic in a sub-interpreter?
- 53:31I mean, if you explicitly work with it, no.
- 53:33But if you're doing the pool, like which one do you get?
- 53:36Is it initialized or not?
- 53:38Eric, do you have an idea of a startup function that runs in the interpreter pool executor type thing?
- 53:44Or is it just they get doled out and they run what they run?
- 53:47With concurrent features, it's already kind of a pattern.
- 53:54You have an initialized function that you can call that'll do the right thing.
- 53:58And then you have your task that the worker's actually running.
- 54:05So with the, I don't know, I wouldn't say it's non-deterministic unless you have no control over it.
- 54:14I mean, it's if you want to make sure that state progresses in an expected way, then you're going to run your own sub-interpreters, right?
- 54:23But if you have no control over the sub-interpreters, you're just like handing off to some library that's using sub-interpreters, I would think it'd be somewhat not quite so important about whether it's deterministic or not.
- 54:35I mean, each time it runs, there are a variety of things.
- 54:40The whole thing could be kind of reset or you could make sure that anything that runs it, any part of your code that runs is careful to keep its state self-contained and therefore preserve determinist behavior that way.
- 54:58One thing I do a lot is I'll write code that'll say, you know, if this is already initialized, don't do it again.
- 55:04So I talked about like the database connection thing.
- 55:07If somebody were to call it twice, it'll say, well, it looks like the connection's already not none.
- 55:11So we're good.
- 55:13Right.
- 55:13You could just always run the startup code with one of these like short circuit things that says, hey, it looks like this interpreter, this is already done.
- 55:20We're good.
- 55:21But, you know, that would probably handle a good chunk of it right there.
- 55:25But we're back to this thing that Anthony said, right?
- 55:28Like we're going to learn some new programming patterns potentially.
- 55:31Yeah.
- 55:32Quite interesting.
- 55:33So we talked at the beginning about how sub interpreters have their own memory and their own module loads and all those kinds of things.
- 55:40And that might be potentially interesting for isolation.
- 55:42Also kind of tying back to Chris's comment here, this isolation is pretty interesting for testing.
- 55:48Right, Anthony?
- 55:49Like my test.
- 55:50So another thing you've been up to is working with trying to run pie test sessions in sub interpreters.
- 55:56Tell people about that.
- 55:57Yeah.
- 55:58So I started off with a web worker.
- 56:00One of the things I hit with a web worker was that I couldn't start Django applications.
- 56:07And realized the reason was the date time module.
- 56:13So the Python standard library, some of the modules are implemented in Python.
- 56:18Some of them are implemented in C.
- 56:20Some of them are a combination of both.
- 56:22So some modules you import in the standard library have like a C part that's been implemented in C for performance reasons typically.
- 56:31Or because it needs some special operating system API that you can't access from Python.
- 56:36And then the front end is Python.
- 56:38So there is a list basically of standard library modules that are written in C that have some sort of global state.
- 56:48And then the core developers have been going down that list and fixing them up so that they can be imported from a sub interpreter or just marking them as not compatible with sub interpreters.
- 57:00One such example was the read line module that Eric and I were kind of working on last week and the week before.
- 57:08Read line is used for, I guess, listening to like user input.
- 57:13So if you run the input built in, like read line is one of the utilities it uses to listen to keyboard input.
- 57:20If you start, let's say you started five sub interpreters at the same time and all of them did a read line listened for input.
- 57:27Like what would you expect the behavior to be?
- 57:29Which when you type in the keyboard, where would you expect the letters to come out?
- 57:35So it kind of poses an interesting question.
- 57:37So read line is not compatible with sub interpreters, but it discovered like it was actually sharing a global state.
- 57:46So when it initialized, it would install like a callback.
- 57:50And what that meant was that even though it said it's not compatible, if you started multiple sub interpreters that imported read line, it would crash Python itself.
- 58:00The date time module is another one that needs fixing.
- 58:04It installs a bunch of global state.
- 58:07So yeah, date time was another one.
- 58:10So what I wanted to do is to try and test some other C extensions that I had and just basically write a pytest extension, a pytest plugin, I guess,
- 58:22which you've got an existing pytest suite, but you want to run all of that in a sub interpreter.
- 58:28And the goal of this is really that you're developing a C extension, you've written a test suite already for pytest, and you want to run that inside a sub interpreter.
- 58:40So I'm looking at this from a couple of different angles, but I want to really try and use sub interpreters in other ways, import some C extensions that have never even considered the idea of sub interpreters and just see how they respond to it.
- 58:54Like read line was a good example.
- 58:57Like I think it was a, this won't work, but the fact that it crashed is bad.
- 59:02How is it going to crash, right?
- 59:03Like what's happening there?
- 59:05Yeah.
- 59:05So it should have, it should have kind of just said, this is not compatible.
- 59:09And that's kind of uncovered a, and this is all super experimental as well.
- 59:14So like, this is not, you know, you've, you've had to import the underscore XX module to even try this.
- 59:21So yeah, there's, there's read line date time was another one.
- 59:26And so I put this sort of pytest extension together so that I could run some existing test suites inside sub interpreters.
- 59:33And then the next thing that I looked at doing was CPython has a huge test suite.
- 59:40So basically how all of Python itself is tested, the parser, the compiler, the evaluation loop, all of the standard library modules have got pretty good test coverage.
- 59:54So like when you compile Python from source or you make changes on GitHub, like it runs the test suite to make sure that your changes didn't break anything.
- 1:00:04Now the next thing I kind of wanted to look at was, okay, can we, to try and kind of get ahead of the curve really on sub interpreter adoption.
- 1:00:14So in 3.13 when PEP 7.3.4 lands, can we try and test all of the standard library inside a sub interpreter and see if it has any other weird behaviors.
- 1:00:26And this test will probably apply to free threading as well, to be honest, because I think anything that you're, you're doing like this, you're importing these C extensions, which always assume that there was a big GIL in place.
- 1:00:40If you take away that assumption, then you get these strange behaviors.
- 1:00:44So yeah, the next thing I've been working on is basically running the CPython test suite inside sub interpreters and then seeing what kind of weird behaviors pop up.
- 1:00:54I think it's a great idea because obviously CPython is going to need to run code in a sub interpreter, run R code, right?
- 1:01:00So at a minimum, the framework interpreter, all the runtime bits, that should hang together, right?
- 1:01:06Yeah.
- 1:01:07There are some modules that it doesn't make sense to run in sub interpreters.
- 1:01:10Readline was an example.
- 1:01:11Some TK interrupt, maybe?
- 1:01:13Yeah.
- 1:01:14Yeah, possibly.
- 1:01:15Maybe not, actually.
- 1:01:17I don't know.
- 1:01:17Yeah.
- 1:01:18If you think about like, if you got, when you're doing GUI programming, right, you're going to have kind of your core stuff running the main thread, right?
- 1:01:27And then you hand off, you may have sub threads doing some other work, but the core of the application, think of it as running in the main thread.
- 1:01:36I think of applications in that way.
- 1:01:38And there are certain things that you do in Python, standard library modules that really only make sense with that main thread.
- 1:01:46So supporting those in sub interpreters isn't quite as meaningful.
- 1:01:51Yeah.
- 1:01:52I can't remember all the details.
- 1:01:54So I feel like there are some parts of Windows itself, some UI frameworks there that required that you access them on the main program thread, not on some background thread as well, because it would freak things out.
- 1:02:05So it seems like not unreasonable.
- 1:02:07Yeah, same is true.
- 1:02:08Like the signal module, I remember at exit, a few others.
- 1:02:12Excellent.
- 1:02:13All right.
- 1:02:13Well, I guess let's, we're getting short on time.
- 1:02:15Let's wrap it up with this.
- 1:02:17So the big thing to keep an eye on really here is PEP 734, because that's when this would land.
- 1:02:24You're no longer with the underscore XX sub interpreter.
- 1:02:28You're just working with interpreters sub module.
- 1:02:32Yeah.
- 1:02:32313.
- 1:02:33Yeah.
- 1:02:33So right now it's in draft.
- 1:02:35Like, what's it looking like?
- 1:02:37If it'll be in 313, it'll be in 313 alpha something, some beta something.
- 1:02:42Like, when is this going to start looking like a thing that is ready for people to play with?
- 1:02:46So I, yeah, this PEP, I went through and did a massive cleanup of PEP 554, which is why I made a new PEP for it.
- 1:02:55I simplified a lot of things, clarified a lot of points, had lots of good feedback from people and ended up with what I think is a good API, but it was a little different in some ways.
- 1:03:06So I've had the implementation for PEP 554 mostly done and ready to go for years.
- 1:03:12And so it was a matter, it's been a matter of now that I have this, this updated PEP up, going back to the implementation, tweaking it to match, and then making sure everything still feels right.
- 1:03:25I try and use it in a few cases.
- 1:03:28And if everything looks good, then go ahead and I'll start a discussion on that.
- 1:03:33I'm hoping within the next week or two to start up a round of discussion about this PEP.
- 1:03:38And hopefully we won't have a whole lot of back and forth so I can get this over to the steering councils in the near future.
- 1:03:45Well, the hard work has been done already, right?
- 1:03:48Yeah.
- 1:03:49The C layer is there and it's accepted and it's in there.
- 1:03:52Now it's just a matter of what's the right way to look at it from Python, right?
- 1:03:56And one thing to keep in mind is that I'm planning on backporting the module to Python 3.12 just so that we have a print interpreter Gale in 3.12.
- 1:04:07So it'd be nice if people could really take advantage of it.
- 1:04:11So for that one, we'd have to pip install it or would it be added as...
- 1:04:14Yeah.
- 1:04:14Pip install.
- 1:04:15Okay.
- 1:04:15I probably won't support before 3.12.
- 1:04:18I mean, subinterpreters have been around for decades, but only through the C API.
- 1:04:23But that said, I doubt I'll backport this module past 3.12.
- 1:04:28So just 3.12 and up.
- 1:04:30That's more than I expected anyway.
- 1:04:32So that's pretty cool.
- 1:04:33All right.
- 1:04:34Final thoughts, you guys.
- 1:04:35What do you want to tell people about this stuff?
- 1:04:38Personally, I'm excited for where everything's going.
- 1:04:40It's taken a while, but I think we're getting to a good place.
- 1:04:45It's interesting with all the discussion about no-gil, it's easy to think, oh, then why do we need subinterpreters?
- 1:04:51Or if we have subinterpreters, why do we need no-gil?
- 1:04:54But they're kind of different needs.
- 1:04:56The most interesting thing for me is that what's good for no-gil is good for subinterpreters and vice versa.
- 1:05:04That no-gil probably really wouldn't be possible without a lot of the work that we've done to make a per-interpreter GIL possible.
- 1:05:12So I think that's one of the neat things.
- 1:05:16The future's looking bright for Python multicore.
- 1:05:19And I'm excited to see where people go with all these things that we're adding.
- 1:05:24When's the subinterpreters programming design patterns about coming out?
- 1:05:29Yeah, my thoughts are...
- 1:05:33Subinterpreters are mentioned in my book, actually, when it was like Python 3.9, I think.
- 1:05:42Because it was possible then, but it's changed quite a lot since.
- 1:05:45They, I guess, kind of some thoughts to leave people with.
- 1:05:50I think if you're a maintainer of a Python package or a C extension module in a Python package,
- 1:05:57there's going to be a lot more exotic scenarios for you to test coming in the next year or so.
- 1:06:05And some of those uncover things that you might have done or just kind of relied on the GIL with global state,
- 1:06:12where that's not really desirable anymore and you're going to get bugs down the line.
- 1:06:17So I think with any of that stuff as a package maintainer, you want to test as many scenarios as you can
- 1:06:22so that you can catch bugs and fix them before your users find them.
- 1:06:26So if you are a package maintainer, there's definitely some things that you can start to look at now to test.
- 1:06:32That's available in 313 Alpha 2 is at least probably the one I've tried, to be honest.
- 1:06:39And if you're a developer, not necessarily a maintainer,
- 1:06:44then I think this is a good time to start reading up on parallel programming
- 1:06:51and how you need to design parallel programs.
- 1:06:55And those kind of concepts are the same across all languages and Python would be no different.
- 1:07:01We just have different mechanisms for starting parallel work and joining it back together.
- 1:07:05But if you're interested in this and you want to run more code in parallel,
- 1:07:10there's definitely some stuff to read and some stuff to learn about in terms of signals, pipes, queues,
- 1:07:20sharing data, how you have locks and where you should put them, how deadlocks can occur, things like that.
- 1:07:27So all of that stuff is the same in Python as anywhere else.
- 1:07:29We just have different mechanisms for doing it.
- 1:07:31All right.
- 1:07:32Well, people have some research work.
- 1:07:35And I guess a really, really quick final question, Eric, and then we'll wrap this up.
- 1:07:38Following up on what Anthony said, like test your stuff, make sure it works in a sub-interpreter.
- 1:07:42If for some reason you're like, my code will not work in a sub-interpreter and I'm not ready yet,
- 1:07:47is there a way to determine that your code is being run in a sub-interpreter rather than regularly from your Python code?
- 1:07:54Yeah. If you have an extension module that supports sub-interpreters,
- 1:07:59then you will have updated your module to use what's called multi-phase init.
- 1:08:04And that's something that shouldn't be too hard to look up.
- 1:08:09I think I talked about it in the PEP.
- 1:08:10If you implement multi-phase init, then you've already done most of the work to support a sub-interpreter.
- 1:08:17If you haven't, then your module can't be imported in a sub-interpreter.
- 1:08:23It'll actually fail with an import error if you try and import it in a sub-interpreter,
- 1:08:28or at least a sub-interpreter that has its own GIL.
- 1:08:31There are ways to create sub-interpreters that still share GIL and that sort of thing.
- 1:08:35But you just won't be able to import it at all.
- 1:08:39So like the readline module can't be imported in sub-interpreters.
- 1:08:43The issue that Anthony ran into is kind of a subtle side effect of the check that we're doing.
- 1:08:53But really, it boils down to if you don't implement multi-phase init,
- 1:08:57then you won't be able to import the module.
- 1:09:00You'll just get an importer.
- 1:09:02So that's, I mean, it makes it kind of straightforward.
- 1:09:04Yeah, sounds good.
- 1:09:06More opt-in than opt-out.
- 1:09:07Yep.
- 1:09:08Right on.
- 1:09:08All right, guys.
- 1:09:09Thank you both for coming back on the show and awesome work.
- 1:09:12This is looking close to the fetish line and exciting.
- 1:09:15Thanks, Michael.
- 1:09:16Yep.
- 1:09:16See y'all.
- 1:09:18This has been another episode of Talk Python To Me.
- 1:09:21Thank you to our sponsors.
- 1:09:23Be sure to check out what they're offering.
- 1:09:24It really helps support the show.
- 1:09:26Are you ready to level up your Python career?
- 1:09:29And could you use a little bit of personal and individualized guidance to do so?
- 1:09:34Check out the PyBytes Python Developer Mindset program at talkpython.fm/pdm.
- 1:09:41Take some stress out of your life.
- 1:09:43Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:09:49Sentry.
- 1:09:49Just visit talkpython.fm/sentry and get started for free.
- 1:09:54And be sure to use the promo code talkpython, all one word.
- 1:09:58Want to level up your Python?
- 1:09:59We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:10:03Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:10:09And best of all, there's not a subscription in sight.
- 1:10:11Check it out for yourself at training.talkpython.fm.
- 1:10:14Be sure to subscribe to the show.
- 1:10:16Open your favorite podcast app and search for Python.
- 1:10:19We should be right at the top.
- 1:10:20You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:10:25and the direct RSS feed at /rss on talkpython.fm.
- 1:10:30We're live streaming most of our recordings these days.
- 1:10:33If you want to be part of the show and have your comments featured on the air,
- 1:10:36be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:10:40This is your host, Michael Kennedy.
- 1:10:43Thanks so much for listening.
- 1:10:44I really appreciate it.
- 1:10:45Now get out there and write some Python code.
- 1:10:47I'll see you next time.
- 1:10:47Bye.
- 1:10:48Bye.
- 1:10:48Bye.
- 1:10:48Bye.
- 1:10:48Bye.
- 1:10:49Bye.
- 1:10:49Bye.
- 1:10:49Bye.
- 1:10:50Bye.
- 1:10:50Bye.
- 1:10:50Bye.
- 1:10:50Bye.
- 1:10:50Bye.
- 1:10:51Bye.
- 1:10:52Bye.
- 1:10:52Bye.
- 1:10:52Bye.
- 1:10:53Thank you.