Latest / Talk Python To Me / #225: Can subinterpreters free us from Python's GIL?
Transcript
- 0:00Have you heard that Python is not good for writing concurrent asynchronous code?
- 0:03This is generally a misconception, but there is one class of parallel computing that Python is
- 0:08not good at, CPU-bound work running in the Python layer. What's the main problem? It's Python's
- 0:14GIL or global interpreter lock, of course. Yet the fix for this restriction might have been hiding
- 0:18inside Python for 20 years, sub-interpreters. Join me to talk about PEP 554 with core developer
- 0:25Eric Snow. This is Talk Python To Me, episode 225, recorded August 2nd, 2019.
- 0:31Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem,
- 0:49and the personalities. This is your host, Michael Kennedy. Follow me on Twitter, where I'm at
- 0:54M. Kennedy. Keep up with the show and listen to past episodes at talkpython.fm, and follow the
- 0:59show on Twitter via at Talk Python. This episode is supported by Linode and TopTal. Please check out
- 1:06what they're offering during their segments. It really helps support the show. Eric, welcome to
- 1:10Talk Python To Me. Hi, how's it going? It's going really well. It's an honor to have you on the show.
- 1:14We met up at PyCascades and talked a little bit, but this latest work you're doing to address
- 1:20concurrency and parallelism in Python is super interesting. So I'm looking forward to talking
- 1:25to you about that. Well, it's super interesting to me too. Yeah, I can imagine. I'm glad you're
- 1:30interested. This kind of stuff is, I don't know, there's just something that draws me in and I'm
- 1:33really enjoy exploring it. But before we do, let's start with your story. How'd you get into programming
- 1:38Python? Oh boy. I had all sorts of ideas on what I wanted to do growing up and computers was not
- 1:46really one of them. But then I ended up at school and somehow ended up signed up for computer stuff,
- 1:55ended up getting a CS degree. And then it's funny because I actually, while I was in school, I was
- 2:01working for a web hosting company doing technical support. Once I graduated, I moved over to a development
- 2:06team. And the guy I replaced is, you may know him, he's named Van Lindberg.
- 2:11Okay. Yeah.
- 2:12So it's kind of funny. So I ended up working on this project that Van had been running and ultimately
- 2:20ended up kind of being the tech lead on that project. It was all written in Python. And so I
- 2:26have Van to thank for my introduction to Python.
- 2:29That's really cool. So you went from talking to the customers and helping them with the problems
- 2:34the developers created to creating the problems for the person who took the job.
- 2:38Just kidding.
- 2:40Kind of.
- 2:41Yeah. That's a really great progression, right? Like you sort of get your foot in the tech space
- 2:45and then, you know, you make your way over to kind of running the team. That's great.
- 2:49It was a good experience. One neat thing is that it was pretty flexible as far as the job goes.
- 2:55There were only a handful of us on the team and we're doing a pretty big job, but we had taken an
- 3:01approach of highly automating stuff. So it was mostly just a matter of making the effort to address
- 3:07automation stuff, which meant that otherwise we had a little more time to kind of delve into issues and
- 3:15solve problems in a better way. And as part of that, whenever I'd run into Python stuff, like I couldn't
- 3:22figure out what was going on or I wanted to understand how something worked, I had the time
- 3:26to go and explore it. And, you know, within a year or something, I discovered the mailing lists.
- 3:32And then, you know, before long, I was actually active in email threads and, you know, I started
- 3:39getting involved with the import system. And by 2012, so this is over the course of a few years,
- 3:45I got commit rights and was pretty heavily active in core development.
- 3:50That's so cool. I think there's something special for working for a small company. You get to touch a
- 3:55lot of different things. You get this freedom that you're talking about to kind of solve the problems
- 3:59the way you see they should be solved and then go and, you know, kind of explore. Right. I work for a
- 4:05small company and I think I really attribute like being in that company in the early days to like a lot
- 4:10of the success of my career because it gave me a chance to like round out the corners that I didn't
- 4:14really know. I wasn't like just pigeonholed into like some super narrow role. Right. You work on what
- 4:19this button does. That's your job. Like, right. Right. Right. Exactly. That reminds me what you
- 4:25just said. That's been my experience with CPython that as I've gotten involved in the mail list and
- 4:30the bug tracker and everything, I feel like is really rounded me out a lot because I'm exposed to
- 4:37so many things, you know, the breadth of programming and just 90% of it, I probably never really would have
- 4:44been introduced to because probably isn't that interesting to me, but because, you know, their
- 4:49email threads and whatever, you know, I learned about it and that's, it's really made a huge
- 4:56difference for me, I think. Yeah. I can imagine. It's just, it's an amazing group of people working
- 5:00on there and then get down into the technical details. Oh yeah. So you started out and in this
- 5:05web hosting company and now you work for a really big web hosting company, right? Yeah.
- 5:09With Azure? Oh yeah. No, not exactly. But definitely, definitely I got some web hosting
- 5:14going on. What do you do day to day over at Microsoft? So I work with Brett Cannon on the
- 5:19Python extension for VS Code. I joined the team a little over a year and a half ago.
- 5:23Nice. That's got to be a fun team to work on. You know, the excitement around VS Code is massive,
- 5:28right? It's, you know, I always ask this question, what's your favorite editor? What editor do you use?
- 5:32Things like that in the show. And yeah, VS Code is definitely tracking as a major thing. And
- 5:38it used to sometimes be Sublime or Atom or something. It's, it seems like certainly for like that type
- 5:46of interface, what would you call it? So what would you call it? I mean, it's, it's not an IDE
- 5:50really. It's not like a terminal app. What, what category of editor is our Sublime Atom VS Code?
- 5:56What's the name? What should I be calling these things?
- 5:58I don't know why. Like a full featured editor?
- 6:02Yeah, exactly.
- 6:04You can't call it an IDE because that's, that's verboten.
- 6:07Yeah. And there's not enough buttons. It needs more buttons and windows, right?
- 6:11It needs more menus and more stuff so you can get lost in there, right? Right now.
- 6:15Yes, exactly.
- 6:16It's too easy not to get lost.
- 6:17Yeah. There's not enough floating windows. Congratulations. I'm sure that's a super
- 6:20exciting thing to be working on and it's, it's really growing quickly.
- 6:23No, it's funny. This is a team that I first talked with them about getting on the team
- 6:28in 2014 and it almost happened. And then there were some complications because I was only going
- 6:34to work remote. At the time I was working for Canonical who makes Ubuntu. So I ended up just
- 6:40kind of waiting and it took like what, two, three years or something like that. But it worked
- 6:46out in the end. But that's, it's kind of a story of my life. I just kind of find a good
- 6:51thing and then wait for it to work out. I'm not, I'm never really in a big hurry, which,
- 6:56which I suppose we'll talk about relative to the stuff I've been working on.
- 7:00Yeah, absolutely. So, well, that's, that's really good. And that's, that's a great project
- 7:06to be working on day to day. And you said that Microsoft actually gives you a decent amount
- 7:10of time to focus on CPython as well.
- 7:12Yep.
- 7:13As they do with Brett and some other folks. And that's, that's really quite, quite cool.
- 7:17Yeah. I get to work basically my Fridays. I work on exclusively on Python. So that's been
- 7:25a big boost to, to what I've been able to get done.
- 7:28That's awesome. So you're saying the fact that we scheduled this on Friday is actually
- 7:31cutting in your time to make Python better for everyone.
- 7:34You know, it's actually part of why I just, yeah, no, it's cool.
- 7:37That's the cost.
- 7:38Yeah. But I think awareness of what you're doing is really good because I think it can make a
- 7:42big difference. So let's just talk about parallelism and concurrency and asynchronous
- 7:46programming and stuff kind of in general and in the, in the Python space. I feel like there's a lot
- 7:52of people who look at what Python does with async. They see things like the GIL and they say, well,
- 7:58this just doesn't work for parallelism. I'm switching to go or some, you know, some,
- 8:03something like that. And I feel like it's, you know, there may be situations where you got to
- 8:09switch to see you, you got to switch to go, but they're like 1% of the situations where people
- 8:13actually do that. Right. Like most of the time, I think that's just not taking advantage of what's
- 8:18out there. So maybe like, let's just set the stage with talking about like concurrency in general.
- 8:23Yeah, you bet. If you look at it, the history of computing, other than really large systems,
- 8:29most computers have been single processor, single core until relatively recently.
- 8:34Yeah. Like 2005 or so, it just used to be the clock speed went up and up and up.
- 8:40And that was how computers got faster.
- 8:42So it's kind of funny because threading, sure, you can kind of logically program for different threads,
- 8:48but ultimately it was just a single thread getting switched by the OS. And that's kind of the,
- 8:56what you had to deal with, but it's a different story now.
- 8:59Yeah. And back in the early days, you didn't even have preemptive multi-threading.
- 9:03Oh yeah. Cooperative multi-threading, like you had to like give it up. Right. It was like in
- 9:08Windows 3.1 in the early days, there was some weird stuff where like you had to be a good citizen on
- 9:13the operating system to even allow that. We're kind of full circle here with async.
- 9:16Yeah. Async it away is exactly the same thing.
- 9:18Yeah. So it's kind of a conceptually the same. So it's really interesting, but now not only do we
- 9:24have concurrency where you have to deal with matters of who's running at a given time, but now we also
- 9:32have parallelism, which gives us performance boosts. But of course with Python, it's an issue with the
- 9:39GIL, which everyone likes to complain about.
- 9:41Right. Exactly. So within a single process, you can't really, unless you are doing certain operations
- 9:48that release the GIL, you can't really run more than one interpreter instruction at the same time.
- 9:53Right. Right. It's really a CPU bound code that suffers.
- 9:56Right. Yeah, exactly. So if you're talking to like databases or you're waiting on web services,
- 10:01all that stuff's fine, right? Like CPython interpreter, once it opens a network socket down
- 10:06the C level, like while it's waiting, we'll release the GIL. And you can do those kinds of things in
- 10:11parallel with threads already. Right. Yeah.
- 10:13Not computationally. Yeah. It's kind of funny because if you look at it and I think Guido's
- 10:17mantra has always been, well, you aren't really hurt by the GIL as much as you think you are,
- 10:22because a lot of code that we write really isn't CPU bound. Very often it's not. And especially
- 10:30for some of the CPU bound stuff, you know, a lot of the critical stuff, people have moved into C
- 10:35extensions anyway. There's still a set of problems that are affected by the GIL. And people have had
- 10:43to work around that number of solutions. You know, asyncio is kind of one thing,
- 10:47but you also have multiprocessing and you have, you know, all sorts of distributed frameworks.
- 10:52Right. Like Dask and other types of things. Yeah.
- 10:54So all that stuff is in part, well, for distribute, it's a little different, but
- 11:00part of the motivation there has just been to leverage parallelism better. So that's one of the
- 11:07biggest complaints that people have with Python. It has been for a while, just parallelism,
- 11:13multicore. And it's a bigger problem now that multiple cores are essentially ubiquitous.
- 11:18Right. Even here on my MacBook, if I go and ask it how many processors it has, how many cores rather,
- 11:25it says it has six and each of those are hyper-threaded. So as far as the OS is concerned,
- 11:29I effectively have like 12.
- 11:31Yeah.
- 11:31And yet it's super difficult to take advantage of those.
- 11:35Yeah. Yeah.
- 11:35In Python.
- 11:36Yeah. Yeah. It's just, it's really interesting. So it's funny the way things have gone and it's,
- 11:41it's going to go even more, more this way. I mean, I expect that the way people program
- 11:46will be different as we think about multiple cores more, but maybe not. I mean, because how often are
- 11:52we writing, you know, CPU bound code?
- 11:55I feel like there's just a couple of situations where it really matters and there are already to
- 12:00some degree, some escape hatches, right? So the most obvious place where in Python,
- 12:06it really matters for computational parallelism is in data science, right? Like I've got a billion of
- 12:11these things. I want to run some crazy algorithm on it and like machine learning training or whatever.
- 12:16But a lot of the libraries that the machine learning folks already have, have some capability
- 12:22for, or for data science folks have, have some capability for parallelism at their lower C levels
- 12:29anyway, right?
- 12:30Yep. That's exactly right. I mean, a lot of these libraries have C extensions where they need them.
- 12:35Exactly. The other place where I feel like you really could get a lot better support is on the web.
- 12:43Yeah.
- 12:43Right. Like we have some of the newer frameworks, we have Molten and Jepronto and Starlette and all these things,
- 12:50Responder that let us write async def, some web method and truly leverage the asyncio components there.
- 12:59But, you know, the main ones, Flask, Django, others, Pyramid, whatever, they don't, right?
- 13:06They're all WSGI based and it's, you just can run into issues, right?
- 13:09I mean, I know the web servers themselves have some capability just to parallelize it out, but it's still, it's, it would be much easier if you did.
- 13:16So I don't think it's that big of a problem.
- 13:18Like there's these two areas, the data science space, and I think the sort of like high-end web serving space that could be handled a little bit better.
- 13:26Yeah.
- 13:26We're already seeing some stuff with async and away on the web, which is, I think, where it's appropriate.
- 13:30I think there's one important caveat too, and it's something that we, we don't really bring up a whole lot in the community, which is that there are a lot of enterprise users of Python that we never hear about how they're using it.
- 13:43In part because of, you know, competitive advantage and that sort of thing, but we don't really hear about it.
- 13:49Yeah.
- 13:49Or they just, they just don't go to the conferences and they don't like spend all their time on Twitter.
- 13:53They just see it as a job.
- 13:55They do their work.
- 13:56They go home.
- 13:56Like they don't, not also their hobby necessarily.
- 13:58Yeah.
- 13:58Yeah.
- 13:59So in a lot of those cases, performance matters and not just performance, of course, efficiency and, and that sort of thing.
- 14:07I mean, it really adds up.
- 14:09So I'm sure there are a lot of folks that we don't even think about who would benefit from better multi-core support in CPython.
- 14:18But, you know, we just, we don't hear about those folks.
- 14:22Well, maybe that's not even them directly.
- 14:24Right.
- 14:24Yeah.
- 14:25Maybe they, they pip install a thing and that thing now works better and they don't even know that it's using multi-core support.
- 14:30Right.
- 14:31But somebody who's really clever found a way to make something they were doing much, much better using that.
- 14:37Right.
- 15:07This work that you're approaching basically tries to deal with this limited,
- 15:37implementation of the Python's guild, the global interpreter lock, which basically has the effect of what I said before, that only a single interpreter instruction can run at a time.
- 15:46Maybe some low level C stuff can also happen, but like the stuff that you write runs, you know, only one like bytecode instruction at a time, basically.
- 15:54Yeah.
- 15:54So maybe just tell people like, that sounds bad.
- 15:59But it's here for a reason, right?
- 16:02It solves a lot of problems for us, right?
- 16:04Oh yeah.
- 16:05It hides a lot of problems that you face when dealing with threads that you don't have to worry about in Python.
- 16:16But not only that, it's also when you're writing C extensions.
- 16:19In C, you have to do a lot of stuff yourself.
- 16:23And when you're dealing with threads, you have to deal with all that.
- 16:27So when you're using Python and you're holding the GIL, you don't have to worry about other Python threads.
- 16:34You don't have to manage your own locks for those threads, which is, I think, makes a threading at the C level in the C API easier.
- 16:43But also, there's a lot of implementation details in CPython that depend on the fact that the GIL protects them.
- 16:52We deal with re-entrancy a lot.
- 16:54But other than that, we don't really have to worry about race conditions on any of the C types, the built-in types or any of those, because they're protected by the GIL.
- 17:05Yeah, which is great.
- 17:06And the GIL is largely a memory management thing.
- 17:09It's not initial job.
- 17:11I mean, it is for threading, but it's mostly to protect the memory management and making that thread safe, right?
- 17:16In large part, it's to protect the runtime state, especially memory management.
- 17:21Yeah.
- 17:21Yeah.
- 17:21So it serves this important role.
- 17:24I mean, we still do have R-lock and things like that, because we might write algorithms that a whole bunch of different steps can't be interrupted, temporarily invalid state or whatever.
- 17:34So we might have to think.
- 17:36But it's very rare, actually, that you end up doing locks and stuff.
- 17:39And other languages like C++ or C# or something, it's common to do locking all over the place, right?
- 17:46For all kinds of funky things.
- 17:48So it's nice that it's not there.
- 17:50And there have been several attempts to take it out to switch to other types of memory management, other things that let us avoid it.
- 18:00But it's always had these problems of making the C extensions not working well or breaking them, of actually making the single threaded case slower.
- 18:11It's one thing to say, okay, we could switch to some other system that's not using the GIL, but now your code's 1.5 times slower.
- 18:18Unless you send like six cores on it, then now it's faster, sort of, sometimes.
- 18:23Like, that's not a great solution either, is it?
- 18:24One of the key things that we protect with the GIL is ref counts.
- 18:29Because we use ref counting for our, essentially for memory management, then we have to keep those ref counts safe from race conditions.
- 18:39So we would have to do locking around all ref count operations, and that would get really expensive real fast.
- 18:45Right, exactly.
- 18:45There have been other projects in the past.
- 18:47Several people have tried to get rid of the GIL, including most recently Larry Hastings with the Gilectomy.
- 18:55And each time it comes down to having to add a lot of locks or similar mechanisms to protect those global resources.
- 19:04And those things kind of fall apart and cause performance issues that ultimately kind of kill the goals of the project.
- 19:13Right, or break the C APIs.
- 19:15Yeah, yeah.
- 19:16If you're looking for performance and you're like, well, we made the Python bit 1.5 times faster, but the C parse doesn't work.
- 19:23Like, all of a sudden, it's much slower, right?
- 19:25Like, that's a problem.
- 19:27If we were able to just break the C API, or even get rid of it and use something else, then we'd be able to solve this problem, I think, without a lot of trouble.
- 19:37But because people in C extensions rely on a lot of these details, we just can't get rid of them that easily.
- 19:44There has been recognition of this kind of in the core team in the last few years,
- 19:50and a recognition that we really got to figure out how to solve this.
- 19:55So I'm hopeful that we're going to figure this out.
- 19:59There have been a lot of smart people thinking about this and a lot of good ideas over the last year or two.
- 20:04There are some things that will have to break, but I think we'll be able to sort it out.
- 20:08That's good.
- 20:08Let's talk about the proposal that you've been working on, PEP 554, which has this concept of a sub-interpreter.
- 20:17And when I heard about this, I thought, wow, okay, this is like some creation that's going to be like this new thing that allows for isolation so you can effectively mimic what you're doing with sub-processing or multi-processing,
- 20:34but without actually the overhead of processes and the inter-process communication.
- 20:38I'm like, okay, this is great.
- 20:39But then as I looked into it, this is not a new idea at the very core of it, right?
- 20:45No.
- 20:45But it's just not something anybody's leveraged.
- 20:47Tell us about it.
- 20:47It's interesting.
- 20:48It really is.
- 20:49Nick Coghlan kind of expressed it as the isolation of processes with the efficiency of threads.
- 20:58And it's not a pure explanation, but it's pretty close.
- 21:03Sub-interpreters have been around as part of Python.
- 21:06Originally, CPython was just implemented as kind of a blob of state, and there was an effort to kind of bring a little sanity to that
- 21:14and isolate all of the state related to Python threads in one C struct and interpreters, which can have multiple threads, in another C struct.
- 21:27And then there's run type state still all over the place.
- 21:30That's just global.
- 21:32So at that point, that was, I don't know, 20, 21, 22 years ago, something like that.
- 21:39And at that time, C API was added for creating and destroying sub-interpreters.
- 21:46And the threading API is built around sub-interpreters to an extent.
- 21:53But it's funny because, like you said, it's not a new thing.
- 21:57And yet, a lot of core developers didn't even know about sub-interpreters.
- 22:01Very few users knew about it.
- 22:04I knew of only one project that was actually using sub-interpreters meaningfully up until four or five years ago.
- 22:12And that was Mod Whiskey, Graham Dumbledon.
- 22:14And it's funny because sub-interpreters now, there's more awareness and people are starting to use them more, and some big projects including.
- 22:22And at the same time, a lot of old users, so Graham, and I've since heard from a few people that use sub-interpreters internally for a long time.
- 22:32Now that we're fixing all the problems with them, they're actually moving off of sub-interpreters because they gave up.
- 22:38It's like, no, just wait another year.
- 22:40We'll probably have a lot of this stuff.
- 22:42Yeah.
- 22:43And you can benefit from performance improvements that we're doing.
- 22:46So it's really funny.
- 22:47A lot of people just didn't know about it.
- 22:49And the people who did didn't really think about it all that much.
- 22:52But it's funny, as C-Python progressed, things would git added in, and they would affect sub-interpreters, but nobody would realize it.
- 23:01There wasn't good tests of sub-interpreters.
- 23:02There weren't many users, so nobody would report problems.
- 23:05Poor Graham, he'd report things, and nobody would really pick up the bugs and work on them.
- 23:11Well, this guy's crazy.
- 23:12What's he talking about, this weird sub-interpreter?
- 23:14Yeah.
- 23:14Is that even a thing?
- 23:15Exactly.
- 23:16There are a number of problems.
- 23:17In my opinion, it never really was quite finished, because they're not as isolated as they probably should be.
- 23:25And there are a number of other rough corners, bugs, and stuff.
- 23:30So what's interesting is the stuff I'm doing, one consequence is that those things have to get fixed.
- 23:37Yeah.
- 23:38So the idea is to lift this concept of a sub-interpreter up out of the C layer, create a standard library module called interpreters, that allows you to program against this concept of the sub-interpreter.
- 23:51Correct.
- 23:52So it's definitely, I'm doing this with isolation in mind.
- 23:56You know, at first, the proposal was just wrap the C-API in Python in a C extension and call it good.
- 24:04Because it's there, right?
- 24:06And somebody early, early on pointed out, well, if you can't share stuff between sub-interpreters, all you can do is just start one up.
- 24:14It's not really nearly as useful.
- 24:17In C, you just do the C thing, you know, pass stuff around however you want and shoot yourself in the foot if you want.
- 24:24Here's a bunch of pointers.
- 24:25Uh-huh.
- 24:25You can talk to them all you want.
- 24:26Exactly.
- 24:27Make sure you don't talk to them at the same time.
- 24:28Don't hurt yourself.
- 24:30Yeah, exactly.
- 24:31But in Python, you know, we don't have the opportunity, which is, I think, a good thing here.
- 24:35So they're like, yeah, well, it's not nearly as useful as it would be if you had just at least some basic way of sharing data between them.
- 24:43So I was like, oh, yeah, that's a good point.
- 24:45And so really got me thinking about sub-interpreters more than just as a tool to achieve other goals, which I expect we'll talk about, but also as actually a vehicle to a concurrency model that I think fits the human brain better, at least in my opinion.
- 25:03I'm not a big fan of async.
- 25:05I'm sure it's great.
- 25:07Some people really get it.
- 25:09For me, it's just, it's, I don't like it.
- 25:12Yeah.
- 25:12But, you know, that's fine.
- 25:13I think there are other ways of thinking about concurrency that work a lot better.
- 25:17Things have been studied since the 60s.
- 25:20Right.
- 25:20Message passing and some of these types of concepts where you're more explicitly like, I'm going to send this over to the thread and the thread's going to pick it up and work on it or things like this, right?
- 25:30Yeah, yeah.
- 25:30Before I moved to Microsoft, I was at Canonical for three years working on various projects written in Go.
- 25:37And Go has a concurrency model that's, I would say, loosely based on CSP, which is kind of a concurrency model that was researched and developed since the 60s, especially by a guy named Tony Hoare from over in the UK.
- 25:53Really powerful stuff.
- 25:55And, you know, it has a lot of similar roots with like the actor model.
- 25:59Yeah, exactly.
- 26:00Go is one of these languages that very explicitly controls how concurrency works.
- 26:06And it's part of the language that this data sharing and whatnot happens, right?
- 26:11I don't think it's great what they did because they took CSP and then they broke some of the fundamental ideas behind it, like isolation in these processes, right?
- 26:21I mean, CSP is communicating sequential processes.
- 26:25So the idea is that you have a process that is just, it's like a single threaded program, right?
- 26:31You could break it down into just a linear flow of code, no matter what, deterministically.
- 26:37And then you have a mechanism by which these processes can communicate.
- 26:42Basically, just send messages back and forth.
- 26:44And they block at those points.
- 26:46I'm going to send a message and wait for the other process to pick it up.
- 26:51And then at that point, both processes will move on.
- 26:54So I spent a while trying to figure out really what would be the best way to set up rudimentary communication between subinterpreters.
- 27:07And my experience with Go came about, so I don't know if I just said this, but Go routines, which are kind of the idea of these processes in Go, they're not isolated.
- 27:19So you can share data between them.
- 27:20So basically invalidates a lot of the ideas behind CSP.
- 27:24I mean, it's interesting.
- 27:26So I want to take advantage of the isolation between subinterpreters.
- 27:30And so essentially, you end up with kind of opt-in data sharing or opt-in concurrency.
- 27:38You don't have to worry about races and stuff like that.
- 27:40It's very much kind of like what the multiprocessing communication flow is, right?
- 27:46I'm giving this data over to this other process, and then they can just have it and they own it and don't have to worry about it.
- 27:52Or they get a copy of it or something like that.
- 27:54So I looked for a lot of prior art that kind of followed this model.
- 27:57And the stuff in multiprocessing was one.
- 28:00The queue module has a lot of stuff that's kind of a similar idea.
- 28:05And there are a few other things out there.
- 28:07And of course, in other languages, I really stuck with this idea of following the model of CSP as much as I could.
- 28:15And really, while the proposal isn't like some CSP implementation, the whole thing is kind of with CSP in mind.
- 28:24Okay, could I build like a nice CSP library on top of this?
- 28:28Right.
- 28:28Because like you said, without the communication, like you said, it's like, it's kind of interesting, but it's just like a task spawning type of thing.
- 28:36Right.
- 28:36Like it's not really any sort of cooperation.
- 28:42This portion of Talk Python To Me is brought to you by TopTal.
- 28:45Are you looking to hire a developer to work on your latest project?
- 28:48Do you need some help rounding out that app you just can't seem to get finished?
- 28:51Maybe you're even looking to do a little consulting work yourself.
- 28:54You should give TopTal a try.
- 28:56Maybe you've heard we launched some mobile apps for our courses over on iOS and Android.
- 29:00I use TopTal to hire a solid developer at a fair rate to help create those mobile apps.
- 29:05It was a great experience and I can totally recommend working with them.
- 29:07I met with a specialist who helped figure out my goals and technical skills required.
- 29:12Then they did all the work to find the right person.
- 29:14I had a short interview with two folks and hired the second one.
- 29:18Then we released the apps just two months later.
- 29:20I think what we ended up with, which is channels, really basic, but I wanted to keep the PEP as
- 29:36minimal as possible.
- 29:37And I think it really, I came up with a good solution for this.
- 29:41So one of the tricks though, is that because of the isolation, you can't just share objects
- 29:47between sub interpreters.
- 29:48I mean, currently you can at the C layer.
- 29:51But in the Python, I didn't want to give anybody ever the opportunity to share objects, at least
- 29:56not at first.
- 29:57Maybe we can come up with some clever solutions for that.
- 29:59But currently you can't.
- 30:00So, I mean, there's really a limit to what can be shared between sub interpreters as proposed.
- 30:06And I want to keep it as minimal as possible so we can build from there.
- 30:10Yeah, absolutely.
- 30:11Well, one of the really exciting parts of that is one thing that is not shared between sub
- 30:16interpreters is the global interpreter lock, right?
- 30:18Well, currently it is.
- 30:20That's the problem.
- 30:22So right now, sub interpreters do share the gil.
- 30:26So one of the things I'm working on is kind of the bigger problem.
- 30:30And really, I'm trying to tackle this problem of supporting multi-core parallelism in CPython
- 30:36using sub interpreters.
- 30:38So kind of PEP 554 is just a vehicle to make sub interpreters accessible to Python users.
- 30:45But really, the actual goal is to fix sub interpreters, including to stop sharing the gil between sub interpreters, which is kind of crazy.
- 30:53That is crazy.
- 30:54But at that point, then you can say start five threads.
- 30:58Each thread starts a sub interpreter as its startup process.
- 31:02And then all of a sudden, the gil is no longer a problem.
- 31:05Precisely.
- 31:05Potentially.
- 31:06And because you're not sharing the objects, right?
- 31:08You don't have to worry about that.
- 31:09And now you have these channels where you can pass data back and forth in a thread-safe way.
- 31:14That's super cool.
- 31:15It sounds like sub interpreters as they exist don't do that.
- 31:19But that's kind of the ultimate goal.
- 31:20Yeah, yeah.
- 31:21Create this exposure of the API to actually create sub interpreters.
- 31:24Move the gil down so it's one per sub interpreter.
- 31:28And then a way to communicate between them.
- 31:30So that's the Harrier problem, right?
- 31:32Is the gil.
- 31:33What we have is, like I said earlier, we have a whole bunch of runtime state all over the place.
- 31:39So one of the things I did a couple of years ago for this project, there are a bunch of things that we've done.
- 31:44Big things that probably nobody even notices because they're all internal.
- 31:48But one of the things I did was I took all the global state I could find and I pulled it all into one single C struct.
- 31:55So what's neat about this project, the ultimate goal of not sharing the gil between sub interpreters
- 32:00is that it requires just a ton of other things.
- 32:04I think I list out 80 different tasks that are probably not even super fine grained that have to get done in order to make this work.
- 32:13And probably 70, 75 of those are things that are a good idea regardless of the outcome of my ultimate goal.
- 32:22Right?
- 32:23Right.
- 32:23See, Python is going to be cleaner.
- 32:24Uh-huh.
- 32:25If we get to those 75 things and then we're like, oh, it's not going to work.
- 32:29Well, that's okay because, dang, you know, we got some good stuff done anyway.
- 32:34Stuff that we wouldn't have done because we weren't really that motivated.
- 32:37I mean, this is open source.
- 32:39So I have a motivation and other people share some of the motivations.
- 32:43It's really neat.
- 32:44There's a lot of collaboration going on now because not just for the whole sub interpreter thing.
- 32:50Some of the stuff that I need is stuff that other people need for different reasons.
- 32:55And it's working out pretty well.
- 32:57But the whole thing is I took all this state and smashed it into a single struct.
- 33:02And as kind of a side effect, I just want to make sure I didn't hurt performance by doing that.
- 33:08So I ran Python's performance suite and it turned out that I was getting a 5% improvement in performance.
- 33:16Interesting.
- 33:16Do you know why?
- 33:17It's crazy.
- 33:17Well, I expect it's because of cache locality of that struct.
- 33:20That was my first guess as well, right?
- 33:22As you load one element of that struct, everything drags along onto L2 cache or the local cache.
- 33:29And it's just a little quicker, right?
- 33:31You just accidentally do fewer deep memory lookups.
- 33:35Somebody pointed out to me that it probably doesn't have quite the same effect on performance for a PGO build.
- 33:41Where the compiler can optimize the layout of memory and various other things relative to what's hottest in the code, right?
- 33:51Yeah.
- 33:51So it kind of runs the thing through a workload and determines what's the hottest chunks of memory and pushes those together.
- 33:59So you get those same cache locality benefits.
- 34:01So ultimately under a PGO build, probably not the same performance benefits.
- 34:06But I only bring that up because...
- 34:09PGO performance guided optimization.
- 34:10Yes.
- 34:11Thank you.
- 34:11For everyone out there.
- 34:12Yeah.
- 34:12Yeah.
- 34:12Acronyms.
- 34:13Yeah.
- 34:14Yeah.
- 34:15So maybe it's not as big necessarily.
- 34:17Is that theoretical or is that something that is actually done on CPython builds?
- 34:21Yeah, yeah, yeah.
- 34:22People do it.
- 34:23If the one that I brew install, is that one?
- 34:25PGO?
- 34:25I don't know.
- 34:26Optimize?
- 34:27Okay.
- 34:28Yeah.
- 34:28Yeah.
- 34:28Yeah.
- 34:29You get some real benefits from a PGO build.
- 34:31There are lots of these little things.
- 34:33One of the things I needed to happen was there was some work that Nick Coughlin started like four or five years ago to clean up runtime startup.
- 34:43And I needed that because otherwise there were certain things I just couldn't do.
- 34:48So I was blocked on that.
- 34:50So finally, I got around to taking the branch that he had, which is on subversion, and moving it over to Git and then at the same revision.
- 35:02And then I had to rebase it against master and fix all the conflicts and finally got that merge like two years ago.
- 35:09And that was a big thing.
- 35:10And then because of that, we're able to do a lot of really good things with startup that we weren't able to before.
- 35:16So that's a side effect.
- 35:17And there are all these things that are just good.
- 35:20That was one of the larger goals of Python 3 as well is try to like fix the just the cold startup times, right?
- 35:27No.
- 35:27No.
- 35:27One of the goals that we have is to fix startup times so they're at least on par with Python 2, which they weren't.
- 35:34Yeah, that's what I was thinking.
- 35:35Weren't nearly at first.
- 35:36That's what I was thinking.
- 35:36Yeah.
- 35:37So we're mostly on par now.
- 35:39The biggest problem was all the Codex and Unicode stuff.
- 35:42That really hits startup performance.
- 35:45So if you think about subinterpreters, if you start up a new subinterpreter, it has to build all this state.
- 35:50It has to load all these modules.
- 35:51There's a ton of stuff that has to happen, right?
- 35:54So you're going to incur that cost for each subinterpreter.
- 35:57As a consequence of what I'm working on, I want to make sure that startup time is as small as possible.
- 36:02So it's definitely one of the things, maybe not one of the immediate concerns, but kind of one of the relatively low-hanging fruit for this project once I finish this first phase is to go in and do things like make interpreter startup more efficient, whether it's sharing or whatever.
- 36:19And those are things that are a good idea regardless of use of subinterpreters.
- 36:24I mean, just for the main interpreter, getting startup faster is a good idea.
- 36:27And that's something that I want for subinterpreters.
- 36:29And I'm motivated to do it.
- 36:31And I think other people, once subinterpreters get in widespread use, people are going to be like, oh, yeah, this is great.
- 36:37And people are going to be motivated to fix some of these deficiencies in subinterpreters.
- 36:42Yeah, absolutely.
- 36:42And it'll definitely, as it's a little bit of a catch-22, right?
- 36:46You said it wasn't even hardly quite finished because here's this idea.
- 36:50But at the same time, if no one's really using it, why do you care about fixing this thing?
- 36:55And if no one's fixing it, I'm not going to use it because it doesn't quite work.
- 36:58And then here you are in this lock, right?
- 37:01So it reaches a point where, well, for me, it was in 2014.
- 37:04I was having a conversation with somebody at work.
- 37:08And they were saying, yeah, Python's going to die because it doesn't do multicore because of the GIL.
- 37:14And it was just, I don't know, one of those moments, you know, when something hits you so deep, you know, they dig a little too hard.
- 37:23And you're like, okay, fine.
- 37:24Forget you.
- 37:25I'm going to fix this.
- 37:26And I'm never going to hear anybody complain about the GIL again.
- 37:29Yeah.
- 37:29Yeah, absolutely.
- 37:30That's what this project is.
- 37:32That's what I've been working on for several years now.
- 37:34It's basically just to get people to stop complaining about the GIL.
- 37:40I definitely think, you know, rightly or wrongly, it's one of the perceived deep limitations of Python.
- 37:46Yeah.
- 37:46I think wrongly, but I do think that it is perceived to be that like Python is not nearly, is barely appropriate for like parallel computing.
- 37:55Yeah, yeah, yeah.
- 37:55I don't think that's right, but I think that's the perception.
- 37:58Yeah.
- 37:58Outside of a lot of Python, or maybe within it.
- 38:00I think it's a fair perception for a class of users.
- 38:04And as a community, we like to be inclusive.
- 38:07We don't want to leave anybody out.
- 38:08We want to make sure that things work out for folks.
- 38:11It's just being open source.
- 38:13It's nothing's going to happen until somebody cares enough to do something about it.
- 38:18And that happened for me.
- 38:19That's awesome.
- 38:20Yeah.
- 38:20And so here we are.
- 38:21Yeah.
- 38:22So it sounds like around September 2017, you introduced PEP 554 to address this.
- 38:28Probably you've been working prior to that.
- 38:312018, you talked about it at PyCon US at the Language Summit and whatnot.
- 38:36And then also, again, in 2019.
- 38:38And like those, also, it sounds like those experiences were a little bit different.
- 38:41Do you want to maybe recount those for us and tell us how this is being perceived over time?
- 38:46You bet.
- 38:47I mean, I've gotten support from the core team.
- 38:49I think my first post about all of this to the Python dev mailing list was probably 2016, early 2016, I think.
- 38:59And, you know, and there was a lot of discussion about it.
- 39:04And there were really only a handful of people that had any sort of opposition to it, any major concerns, which I took as kind of a valid litmus test on if it was worth pursuing.
- 39:18Yeah.
- 39:18And when you initially presented it, what was the scope?
- 39:21Was the scope like the final end goal where there's like you're trying to use it for concurrency and all that?
- 39:27That was like from the start.
- 39:28Talking about using subinterpreters and not sharing the GIL to achieve these goals of multi-core parallelism.
- 39:36So, you know, at some level to the details, pursuing kind of a CSP model, a standard library module.
- 39:43And, you know, and the response was pretty good.
- 39:45There was several long threads and I incorporate all the feedback into the PEP ultimately.
- 39:51But, yeah, the feedback from the PEP was great.
- 39:54And then come PyCon 2018, I basically asked everybody I talked to, explained subinterpreters and what I was working on and asked them what they thought of it, how they would use it.
- 40:06And it seemed like everybody had a different response.
- 40:09Everybody was excited about it, almost universally.
- 40:12And everybody had a different response on how they would use it.
- 40:17Most people, I didn't even have to ask them.
- 40:20The big guy, go like, wow, I have the perfect use case for that.
- 40:23This, this, this, this.
- 40:24And I even at one point asked, oh, his name escapes me, the maintainer of Dask.
- 40:30Matthew Rockland.
- 40:31Matthew Rockland.
- 40:32You're welcome.
- 40:32I asked him about this.
- 40:33He's like, wow, subinterpreters sound neat.
- 40:36But I doubt I would incorporate, I would make use of them for Dask, except Dask internally has all these control threads and all this machinery built out.
- 40:50For managing all of the distributed programming.
- 40:54Just so people know, Dask is a way to run your Python code potentially on a bunch of different systems.
- 40:59It's kind of like, like a Pandas data frame style programming.
- 41:05But you say run this computation, but like all over the place.
- 41:08That's the problem that Das solves.
- 41:10And then.
- 41:10So it's really interesting.
- 41:11He said, but yeah, I would totally use that for my internal stuff.
- 41:16I mean, for these control threads.
- 41:18I mean, I would totally make use of that because it was, it's perfect.
- 41:21Or, you know, talking to web folks and they're like a bunch of different use cases for how this apply to web frameworks.
- 41:27Or basically everybody had ideas.
- 41:30Yeah.
- 41:31There's a ton of great ways.
- 41:32Yeah.
- 41:32It was really neat.
- 41:33So I got excited and then come the sprints that year.
- 41:37Oh, and one of the people that was really supportive was Davin Potts, who's one of the maintainers of multiprocessing.
- 41:44Which is, you know, I thought that was a pretty good sign that I was in the right direction.
- 41:49It is absolutely a good sign.
- 41:50I mean, this is like the next gen multiprocessing in my mind, kind of.
- 41:54I still wonder if once we have sub interpreters, do we even need multiprocessing?
- 41:58Subprocesses make sense.
- 42:00But does multiprocessing make sense?
- 42:02I'm not sure.
- 42:02I mean, it's kind of like the big hammer to solve the GIL problem and this isolation problem by just going fine, the operating system will do it.
- 42:11But if CPython itself does it, then I mean, maybe there's a memory.
- 42:14But it's interesting to think about.
- 42:16Yeah.
- 42:16What's funny is now after I'm charged up, I'm excited.
- 42:20I've got all sorts of notes.
- 42:21I'm like, wow, you know, there's all this stuff.
- 42:24Several people have said that they'd like to help out.
- 42:28You know, and then I get to the sprints and you can imagine that Guido's a busy guy at PyCon.
- 42:33You know, everybody wants to talk to Guido, taking pictures.
- 42:36He can't get even a moment piece.
- 42:38I know it's, yeah, it's definitely got people chasing him around.
- 42:41It wears him out.
- 42:42And there's always stuff going on.
- 42:44There are people that he needs to talk to about different proposals and whatever.
- 42:48And this is 2018.
- 42:50He's still BDFL.
- 42:51And there's a lot going on.
- 42:55And what happens?
- 42:56He actually comes and finds me, sits me down.
- 42:59And for 45 minutes, he basically tells me that he thinks it's a bad idea.
- 43:06And I can tell you, I walked to, so I understood where he's coming from.
- 43:10And I think in part, he had misunderstood what I was trying to do.
- 43:14Yeah.
- 43:14It's like that telephone game where one person tells a person who tells a person something and
- 43:19it's not the same on the other side.
- 43:21And in the conversation, you know, I tried to clarify a few points, but it really wasn't
- 43:24a great opportunity to try and explain really why this was a good idea.
- 43:29I mean, the PEP to an extent does, but I think there's kind of a gap in the justification that
- 43:38really Guido was just, I hadn't communicated well to him.
- 43:42So 45 minutes.
- 43:43And basically, you know, I conceded some of the points that he made and tried to explain
- 43:49the others.
- 43:50And ultimately, you know, it's not like he said, stop.
- 43:54He basically said he thought it was a waste of my time that I should work on something
- 43:58that's going to benefit people more.
- 44:01Also, he was coming from thinking about the problem in a different way than I was and a
- 44:08different understanding of exactly what I was trying to solve and what I was trying, what
- 44:12the proposal was, what the solution was.
- 44:14Well, he probably also has a lot of Gil fatigue hearing how Gil is ruining Python and all that,
- 44:19right?
- 44:19Yeah.
- 44:19And I think in part, he was just worried that I was going to get people excited about something
- 44:24that wasn't going to actually end up happening.
- 44:26Okay.
- 44:27So it was kind of a bummer.
- 44:28I was bummed out probably the rest of the day.
- 44:31Did you walk away less inspired or are you still excited after all the other input you
- 44:35got?
- 44:36I was still determined.
- 44:37Yeah.
- 44:37I probably, my excitement level was lower only because it'd been suppressed a little, but
- 44:42you know, that wears off.
- 44:44And talking to more people about it, same level of excitement, the same excitement about how
- 44:49they would use it.
- 44:50And so, you know, I didn't worry about it, but I was worried that if I couldn't convince
- 44:55Guido, then A, of course, I didn't think it would happen.
- 44:59And B, maybe it really wasn't a good idea.
- 45:01Because Guido is smart and he's been doing this a long time.
- 45:04And I have absolute trust in that uncanny ability he has to understand whether something
- 45:10is good for Python or not.
- 45:12Yeah.
- 45:12I mean, he's amazing.
- 45:13So it did make me wonder, well, what if he's right?
- 45:17Maybe I'm not understanding.
- 45:19That's probably more likely.
- 45:20So, but I kept at it.
- 45:22I was determined.
- 45:23You know, like I said, I waited three years for the job I have now.
- 45:28So I was like, you know, I'll just keep going.
- 45:31And if nothing else comes of it, I was convinced that 80 or 90% of stuff that I was doing was
- 45:37a good idea regardless.
- 45:38So I was like, I'll just keep going.
- 45:40And if it ends up that it's not going to work out, I won't feel too bad about it.
- 45:45I'll have made a difference, I think.
- 45:47So kept going.
- 45:48But then 2019 rolls around and Guido pulls me aside again and says, oh yeah, that's
- 45:55a good idea.
- 45:55Been thinking about it.
- 45:57Because he got it.
- 45:58Well, the point that we were fixing, he saw over the course of the year, he saw that I was
- 46:02working on all these things that I needed for the goal, but they were a good idea regardless.
- 46:08And he's like, oh yeah, you're working on all this stuff.
- 46:11And also, he probably heard my explanation a few more times and it clicked on how I was trying
- 46:18to solve this problem.
- 46:19And he said, yeah, that could work.
- 46:21And so I was floating around for a while.
- 46:23It was exciting.
- 46:24That's super cool.
- 46:25One of the challenges a lot of peps and projects have had recently, let's say since July 2018,
- 46:32maybe, is we, more like you guys, have not really had a way to decide to make decisions
- 46:41after Guido said, I'm stepping down, I'm just stepping back to standard core developer or
- 46:47steering council now, but stepping back saying, you guys have to figure out a new way to like
- 46:51make decisions and sort of govern yourself.
- 46:54Right?
- 46:54So that, your PEP spanned that gap.
- 46:57So I'm sure that didn't.
- 46:58Oh man, it was brutal.
- 46:59Was it?
- 46:59It literally killed a lot of the momentum I had coming out of PyCon 2018 because that happened
- 47:06just a couple months after.
- 47:09And basically, I kept working on stuff, but there was all these discussions about governance
- 47:14and governance and governance, and it just dominated a lot of what we were working on.
- 47:19So there wasn't a lot of collaboration going on with this project.
- 47:22And there was a lot of just cognitive effort to stay on top of this stuff because it's important.
- 47:28So really, until all this was solved, pep554 was ready.
- 47:35Basically, right after PyCon, I was 2018, I'd worked up kind of a separate list of arguments
- 47:43to make to Guido on why this was a good idea and try and kind of fill that gap that I had
- 47:48perceived.
- 47:48And then on top of that, I had updated the PEP to kind of iron out some of the small things.
- 47:54I felt like it was ready.
- 47:55And literally, right before I was going to ask for pronouncement on the pep, then, or no,
- 48:02I think I was going to wait until the core sprints in September so that I could talk to
- 48:06Guido in person and try and make the case and then ask for pronouncement.
- 48:09So, you know, this was, it was brutal because then no peps got decided.
- 48:16And the core sprint was mostly spent talking about governance stuff, which that's fine.
- 48:21It was productive, but I wasn't able to get a lot of progress.
- 48:24So it just kind of slowed things down so much.
- 48:28And then when we finally got governance ironed out, you know, there's transition.
- 48:32So this whole time I was honestly aiming to solve it, to get PEP 554 landed for three, eight,
- 48:39and then even get the stop sharing the GIL stuff done for three, eight.
- 48:44Neither one happened in large part because of the whole governance issue.
- 48:48It's probably good in the longterm that this transition happened, but in the short term,
- 48:52it definitely threw a bunch of molasses in, you know,
- 48:55it's a little disappointed every release you miss on something.
- 48:59It's a little part of you hurts, but.
- 49:01I can imagine.
- 49:02Well, and the releases are long, like the gaps are wide between them, right?
- 49:0518 months is a long time in technology.
- 49:08It's not like, well, maybe next month it'll come out.
- 49:10We're actually talking about reducing the release cycle to a lot smaller, six or 12 months.
- 49:16Depending.
- 49:17I think that's interesting.
- 49:18What's the trade-off there?
- 49:21So our currently released manager, Rukash, he, he said for three, nine, I want it to be shorter.
- 49:26So he basically said there are a variety of reasons.
- 49:30The main opposition to having shorter release cycles was that it's more of a burden on the release team,
- 49:38but that's less of an issue.
- 49:40Now there's a lot more automation.
- 49:41And so this is coming from the release manager.
- 49:45So he was in a position to determine what made sense.
- 49:49So that's, that's kind of how that's played out.
- 49:52He's, he's like, let's do this.
- 49:54And so there was some discussion for a stretch on what would be the best time.
- 49:57And if it made sense at all, of course, but if we went with it, you know,
- 50:01what kind of release interval we'd have and how that would work logistically and how that would play into other factors of core development.
- 50:10So I don't remember where that's gotten to.
- 50:12I think there was some consensus that it would make sense to look at it further, but I think like most long discussions do, it kind of tailed off without a good conclusion quite yet.
- 50:24I don't know.
- 50:25I don't remember.
- 50:26It's, I don't remember what the PEP number is.
- 50:28It's the release PEP for three, nine is where he started this discussion.
- 50:32So there's a number of threads related to that PEP.
- 50:35You know, to me, it sounds generally positive, right?
- 50:39Like smaller releases that you can understand a little bit more completely rather than just like, here's a huge dump of 18 months of work.
- 50:48But I definitely do understand it.
- 50:50I mean, you've got all the places, all the Linux distributions, all the other places that are shipping it.
- 50:57They have to now think probably about that more frequently.
- 50:59That was definitely one of the concerns.
- 51:01But now that the Linux distributions are moving away from exposing their system Python, that it's less of a concern.
- 51:11Right.
- 51:12So one interesting thing in this discussion was just the idea of moving to Calver for versioning Python.
- 51:19I think that was something that Brett had talked about.
- 51:22So, you know, there are a number of different ideas.
- 51:24Like actually having the version number be like 2019.6 for June or something like that.
- 51:32Yeah.
- 51:32So then you'd end up with 2019.6.0.1, you know, for bug fixes and all that.
- 51:38Definitely.
- 51:38I like the calendar versioning for like packages and stuff.
- 51:42But for the actual core, like that's pretty interesting.
- 51:45I don't know.
- 51:45It may not make sense.
- 51:47There are a lot of things that people talked about.
- 51:50We talked about the possibility of LTS releases or some variation on that.
- 51:55And so that we'd be maintaining multiple.
- 51:57But, you know, I think a lot of people are kind of burnt out on having maintained 2.7 and Python 3.
- 52:05At this point, like, have we just about gotten out of this?
- 52:07Most people don't bother with 2.7 at this point, core developers.
- 52:12So it's really interesting.
- 52:14I don't know.
- 52:15There are lots of ideas.
- 52:16I think ultimately we'll settle on the right thing.
- 52:19Something that'll work well for us.
- 52:21Even if it's a status quo, if we figure out that's the best way forward.
- 52:24But we've already since 3.6, I think it was, we started doing a shorter release cycle, more like 14 months.
- 52:32Because we used to do release cycle from release to release.
- 52:37But now we do, or from final to final.
- 52:40Now we do, if you think about it, it's more like final to beta 1.
- 52:44Right.
- 52:44Which we're already like way past 3.8 beta 1.
- 52:47The final release date for the next version is basically 18 months from beta 1 now instead of final.
- 52:54That's the way we've been doing the last few releases.
- 52:57So it breaks it, shortens it to like 14 months.
- 53:00So 12 months really wouldn't be that different.
- 53:02Yeah, that's true.
- 53:03We'll see what happens there.
- 53:05But, you know, interesting topic.
- 53:06For sure.
- 53:07So the final takeaway is you're targeting Python 3.9, which will be basically where the work is going into now, right?
- 53:14Like you're already in beta of 3.8.
- 53:16It's kind of frozen and whatnot.
- 53:17So it's going to be probably the next version of Python.
- 53:20Maybe that will be shorter.
- 53:22Maybe not.
- 53:22A little undetermined at this point.
- 53:24Might be 12 months from now or who knows.
- 53:27I expect, regardless of when it is, that we're close enough that we'll be able to get all of this sub-interpreter stuff done for that.
- 53:33Assuming PEP55R gets accepted, which I expect.
- 53:37I hope it does.
- 53:38I expect it will.
- 53:39I don't see a reason why it wouldn't.
- 53:41Yeah, it seems like the excitement is there for it.
- 53:43To me, it clearly solves the problem, assuming like the startup time of the sub-interpreters is not just equal to multiprocessing and things like that.
- 53:53It seems like it's going to be really great.
- 53:56Yeah, and what's nice is I've done this in a way that will start really minimal.
- 54:00Like you'll only be able to pass bytes or strings or other basic immutable types between sub-interpreters.
- 54:08But with this foundation, then there's like a whole list of really neat projects that people can work on to improve things for sub-interpreters.
- 54:16Like I talked about earlier, improving startup time, but also things like one neat idea is the idea of for memory allocators in CPython.
- 54:25Right now, we use one memory allocator throughout the whole lifetime of the runtime.
- 54:30Memory allocator is in charge of, of course, allocating and deallocating memory.
- 54:34So what if you could use a different memory allocator per interpreter?
- 54:39Well, what if you could at any arbitrary time swap out an allocator so that objects are allocated using different allocators?
- 54:47Then you could manage relative to the allocators for those objects and you get some neat things.
- 54:54Like what if you had an allocator that was page size, right?
- 54:58And so then you actually can, in Python, have a class that kind of wraps that allocator so that you can create objects relative to that class.
- 55:10Or create an object that represents allocator and then any attribute that you create on the object is in that allocator or whatever.
- 55:20So now you have this self-contained memory page that then you could mark, let's say, read-only.
- 55:27Suddenly, all that memory is read-only and you have truly read-only objects in Python.
- 55:33What if you take that read-only and now you can pass that whole memory page over to another interpreter and you don't have to worry about any race conditions relative to that memory page?
- 55:46One of the best ways to get parallelism is to have immutability.
- 55:49I think there are lots of...
- 55:51And so there's a...
- 55:52I have a project open for this and a number of other resources where I've basically written all this stuff down.
- 55:59Like, here's a whole list of awesome things that we can do once we have this foundation set.
- 56:05Would you get things like maybe less memory fragmentation for long-running processes?
- 56:09If you could start up these sub-interpreters, like give them a block of memory, let them throw that away.
- 56:14And like things like this, like other benefits, possible memory leaks for like badly written code, but like it was all within a sub-interpreter.
- 56:19There are a number of things there.
- 56:22One is that I have a list of...
- 56:26Like I said, there's all this global state all over.
- 56:29This is kind of the main blocker for me right now.
- 56:31And so we have all these static globals in the C code all over the place, thousands of them.
- 56:36Most of them can't be global.
- 56:40So I can't even pull them into the runtime state struct.
- 56:44I have to pull them down into the interpreter state, which means I have to collect them out of static globals and kind of migrate them into this pi interpreter state struct.
- 56:54And it's just a lot of work.
- 56:56And then I have to make sure that nobody adds any static globals that they shouldn't in the future.
- 57:01Or else, same problem all over again.
- 57:04So, I mean, this is probably the main problem right now.
- 57:08Aside from all those globals, there are some parts of the pi runtime state, which is this struct where I pulled in a lot of globals earlier, a couple years ago.
- 57:19There are key items of that struct that I've identified that need to move over into the interpreter state.
- 57:28The gil will be the last one of those.
- 57:30But right before that is memory allocators.
- 57:33So I'm pretty sure that we'll be able to do this just fine.
- 57:37But I need to see how it affects performance.
- 57:40But moving the memory allocators to per interpreter.
- 57:45So I think one of the side effects, I mean, it really could be reducing memory fragmentation down, you know, isolating it to per interpreter.
- 57:53Which, if you're using multiple interpreters, that's a good thing.
- 57:56Yeah, it's really interesting.
- 57:57And certainly the pressure that hardware is putting on top of programming languages and runtimes is not getting less, right?
- 58:05Like, we're only going to have more cores, not fewer going forward.
- 58:09So it's only going to be a problem that stands out more starkly if Python only reasonable runs on like one core at a time.
- 58:17When you have 16, 32, 64 cores, whatever it is in like five years, right?
- 58:22Yep.
- 58:23So it's definitely a good project.
- 58:24I'm really excited about it still after first motivated to work on this and five years ago.
- 58:30You know, I'm still motivated.
- 58:31Almost gave up at one point.
- 58:33But plugging away.
- 58:35And now a lot of people are excited.
- 58:36Looks like it's really going to happen for 3.9.
- 58:38Are some of the other core developers helping you?
- 58:40Somewhat.
- 58:41Everybody's got different goals in mind.
- 58:45Victor Stinners, he's been really helpful for some of this stuff, especially relative to the CPI.
- 58:50I've had offers of help from others.
- 58:54Before Emily Morehouse became a committer, I was helping to mentor her.
- 59:00And one of the things that we did, we met basically weekly.
- 59:04And for the most part, we paired up on working on sub-interpreters.
- 59:09And that was a big help.
- 59:10Now she's...
- 59:11Yeah, that's cool.
- 59:11She's all important now.
- 59:13No, no.
- 59:15Emily's great.
- 59:15But she's so busy.
- 59:17Yeah, she's great.
- 59:18She's running a successful company.
- 59:19It's really busy.
- 59:21And on top of that, she's the chair for PyCon 2020 and 2021.
- 59:27Well, I'm guessing 2021.
- 59:29Anyway, at least next year.
- 59:31And then she's got a lot of this stuff going on.
- 59:33And she did the assignment expression implementation.
- 59:36And all sorts of stuff.
- 59:38But during that time when she was helping out with this stuff, it was a really big help.
- 59:42So lots of help.
- 59:43I had help from a number of folks out in enterprise.
- 59:48Talked to folks at Facebook and Instagram and some other companies.
- 59:53I've had offers to help from other individuals.
- 59:56Help from small companies.
- 59:59People coming up and saying, hey, I want to get my whole team working on this.
- 1:00:04It hasn't really gone anywhere.
- 1:00:05I don't get my hopes up too high.
- 1:00:07Yeah, it's such a big problem, right?
- 1:00:09It's like so wide spanning.
- 1:00:10It sounds like with all the globals and whatnot.
- 1:00:13You got to really...
- 1:00:14It's not very focused.
- 1:00:15So it's hard to work on, I suspect.
- 1:00:16One thing I made sure to do was break this problem down into zillion tasks.
- 1:00:20As granular as I could.
- 1:00:22So I think I gave you the link there to the multi-core Python project that I have.
- 1:00:27If you look me up on GitHub, you'll find that repo.
- 1:00:31And that repo is basically just a wiki and GitHub projects breaking down all this work into discrete chunks.
- 1:00:40I'll certainly link to all those things in the show notes.
- 1:00:42So we'll just click on it.
- 1:00:43But yeah, that's great.
- 1:00:44Link to your...
- 1:00:45You gave a talk at PyCon 2019.
- 1:00:47I don't think we mentioned that yet.
- 1:00:49Yeah.
- 1:00:49So it was a talk.
- 1:00:50I actually proposed two talks.
- 1:00:52One of them was specifically about subinterpreters and both PEPF5-4 and the whole effort to move GIL to per interpreter.
- 1:01:02That got rejected.
- 1:01:03That was the one I wanted to give.
- 1:01:05I gave another one that's broader.
- 1:01:08It was kind of a superset.
- 1:01:10It included the stuff from the other talk, but it also talked about all about the GIL in general.
- 1:01:16The history of the GIL, what really, you know, the technical ideas behind the GIL, really race conditions and parallelism, concurrency and all that stuff.
- 1:01:28And then also talked about what we need to do to kind of solve that problem, including talked about some of the past efforts and also current efforts to make fixes in the CPI, changes in CPI so that we can move past the GIL.
- 1:01:46And then I focus a lot of the talk on the stuff with subinterpreters.
- 1:01:51Cool.
- 1:01:51Yeah, that sounds really interesting.
- 1:01:52We'll definitely link to that.
- 1:01:53All right, Eric, I think we're just about out of time.
- 1:01:57We'll definitely cover this.
- 1:01:58And I'm really excited for this project.
- 1:02:00So if you need any more positive vibes and feedback, I think this definitely has a chance to, like, really unlock this multi-core stuff in a general way.
- 1:02:10I think there's interesting APIs you can put on top of it to even make it, like, almost transparent to folks.
- 1:02:17You know, I'm a big fan of the unsync library, which has a cool unifying view on top of threading, multiprocessing, and async.
- 1:02:24And this would dovetail, like, right into that, like, just a little decorative value.
- 1:02:29Oh, yeah.
- 1:02:29And boom, it's subinterpreter execution and all sorts of stuff.
- 1:02:32That would be great.
- 1:02:33Yeah, it's really awesome.
- 1:02:34Excellent work.
- 1:02:35I'm looking forward to using it in 3.9 beta 1.
- 1:02:38Now, before you get out of here, though, I do have two final questions for you.
- 1:02:41I think we may have spoiled the first response.
- 1:02:44If you're going to write some Python code, what editor are you going to use?
- 1:02:47I think people may be able to guess what you're going to say here.
- 1:02:50You know, it's funny.
- 1:02:51First, I'll say that the Python extension for VS Code is written not in Python, but in TypeScript.
- 1:02:59Because it's an Electron JS app, yeah.
- 1:03:01That's a whole other topic.
- 1:03:02An interesting one.
- 1:03:04So, for the most part, I've been using Vim forever.
- 1:03:08As long as I've used an editor that wasn't on Windows, I've been using Vim.
- 1:03:13And so, you know, naturally, after years, you kind of build up muscle memory, and you build up a whole set of configurations and all that stuff.
- 1:03:23And so, changing editors is hard.
- 1:03:25But given that I work on an extension for VS Code, it's pretty meaningful to actually use VS Code, right?
- 1:03:34Right.
- 1:03:34Just to experience the thing, right?
- 1:03:36It just makes it all that better.
- 1:03:38I really appreciate VS Code.
- 1:03:39I'm not really a big use my mouse while I'm working sort of guy.
- 1:03:43So, VS Code is definitely out of the box oriented towards use your mouse.
- 1:03:49I mean, Windows is.
- 1:03:50So, kind of there's that mentality.
- 1:03:53And that's fine.
- 1:03:54It's definitely, that's a target.
- 1:03:56So, it's not really how I operate all that much.
- 1:03:59There are ways, however, there's like a Vim extension, which basically makes VS Code work like Vim.
- 1:04:06So, I tried it.
- 1:04:07And it was nice.
- 1:04:09There were only a couple problems.
- 1:04:11And they're kind of blockers for me.
- 1:04:13Okay.
- 1:04:13I use VS Code for Python stuff sometimes, but most of the time not.
- 1:04:18Once you know and love an editor, it's tough.
- 1:04:21I think that they're solvable problems.
- 1:04:23And I've kind of pushed the feedback upstream.
- 1:04:25So, who knows?
- 1:04:26I mean, maybe I'll move away from Vim at some point.
- 1:04:29Makes it hard when I'm in a terminal and I need to edit stuff.
- 1:04:32I can't really pop up VS Code.
- 1:04:34Yeah, yeah.
- 1:04:35But you do have that cool of like remote editing stuff that's coming in VS Code, which is pretty cool.
- 1:04:39That was one of the blockers.
- 1:04:40And now that there's that, it's less of an issue for me.
- 1:04:43So, there are only really a couple things left that are kind of blocking me from using VS Code.
- 1:04:49Otherwise, I like it.
- 1:04:50There are a lot of things that I just don't, haven't bothered with them, but you just get out of the box with VS Code.
- 1:04:57And it's nice.
- 1:04:57Cool.
- 1:04:58All right.
- 1:04:59And then notable PyPI package.
- 1:05:01Maybe not the most popular, but something you're like, wow, people should really know about this and maybe they haven't heard of it.
- 1:05:06That's a great question.
- 1:05:07There's a few out there.
- 1:05:09I'm a big fan of projects that really have been able to stay on top of their growth.
- 1:05:14That's a really hard problem when you're working on a project and it gets popular.
- 1:05:18Trying to keep up.
- 1:05:19Most of the time it's just volunteers, spare time.
- 1:05:22Things often grow pretty organically.
- 1:05:24I think for the most part, most programmers are pretty pragmatic.
- 1:05:28So, they aim for immediate fixes.
- 1:05:30So, it's really hard over time to keep a project under control, especially when it gets big.
- 1:05:36So, I'm a big fan of projects that kind of keep that under control.
- 1:05:41There's some projects I think that have aimed for simplicity and really focused on that.
- 1:05:47See, I'm setting myself up for failure here though, because I want to give a good example of this.
- 1:05:52And not having looked at any projects too closely in a while, I may be kind of invalidating my whole point.
- 1:06:00There's some neat ones out there that people find useful.
- 1:06:02Of course, attrs, but attrs is kind of, with data classes now, attrs is, it still has a place, I suppose, but not quite as much as it did.
- 1:06:11Yeah, it definitely seemed to have.
- 1:06:12Did it directly inspire data classes?
- 1:06:14Yeah.
- 1:06:15It's kind of achieved its goal like in a meta way.
- 1:06:17Anyway, yeah.
- 1:06:18I have one on there, ImportLib2, which is a backport of Python 3's ImportLib to Python 2.
- 1:06:24But I haven't really kept up with it, so it probably doesn't work anymore.
- 1:06:29Those are good.
- 1:06:30Yeah, attrs is definitely a good one.
- 1:06:31Backport ones are kind of useful sometimes.
- 1:06:34But there's also, there's some that make it easier to use some of the trickier functionality of Python.
- 1:06:41So, things that deal with descriptors, for instance.
- 1:06:44There's some decorator packages out there.
- 1:06:47I think Graham Dumbledore has wrapped.
- 1:06:49That's an interesting one.
- 1:06:50One that I think people don't think about a whole lot is PSUtil, which actually is really neat because it has some good abstractions cross-platform for a lot of the things that you do system-side.
- 1:07:04Like monitoring processes, getting system information, killing processes, or whatever.
- 1:07:10But it also stays pretty focused.
- 1:07:12I think that's a good one.
- 1:07:13Yeah, PSUtil, that's definitely a good one, yeah.
- 1:07:14PyC parser is one I've looked at recently that does some neat things.
- 1:07:19It allows you to parse C code.
- 1:07:20Pure Python, though.
- 1:07:21Oh, interesting.
- 1:07:22Okay.
- 1:07:23There's some limitations to it, but otherwise, I think it's actually pretty cool.
- 1:07:27Awesome.
- 1:07:28Very cool.
- 1:07:28Those are definitely some good ones.
- 1:07:30All right.
- 1:07:30Final call to action.
- 1:07:31People are excited about this.
- 1:07:32Maybe they want to help out.
- 1:07:34Maybe they want to try or see some of the changes.
- 1:07:36Is there something they can do?
- 1:07:38Is there like this list you talked about?
- 1:07:40Can they find this list to see if they can take one of them for you?
- 1:07:43First of all, if anybody's interested, they can just get in touch with me immediately.
- 1:07:47I'll get right back to you.
- 1:07:49We'll talk about all about the project, how they can help, what their interests are, how that lines up.
- 1:07:55That project I talked about.
- 1:07:56That project I talked about, the link that you'll have, has a lot of the tasks broken down as issues organized on the project board.
- 1:08:05So you can take a look at those.
- 1:08:07Also, the wiki is basically where I've dumped pretty much all of my notes on this stuff.
- 1:08:12Read through there.
- 1:08:13There's lots of stuff.
- 1:08:15You can see how it applies.
- 1:08:16Give feedback on the PEP.
- 1:08:19And there may be other ways that it could work that you've thought of that nobody else did that are worth talking about.
- 1:08:27But again, just get in touch with me.
- 1:08:29It wouldn't take a lot of effort.
- 1:08:30And I can get you working on something right away, something that will interest you and will make a real difference here.
- 1:08:38I think this is a feature that people, until they think about it, don't realize how important it is.
- 1:08:45I really do think that it's going to make a big difference for people.
- 1:08:47That's awesome.
- 1:08:48Great bunch of ways for people to get involved.
- 1:08:49I totally agree with you.
- 1:08:50I've certainly put this in my top five most important projects for Python.
- 1:08:55So very good work.
- 1:08:56I love this deep dive.
- 1:08:57Thanks for taking the time, Eric.
- 1:08:58Yeah, thank you.
- 1:08:59Thanks for having me, Michael.
- 1:09:00Yeah, you bet.
- 1:09:01Bye.
- 1:09:01This has been another episode of Talk Python To Me.
- 1:09:05Our guest on this episode was Eric Snow.
- 1:09:07It's been brought to you by Linode and TopTal.
- 1:09:09Linode is your go-to hosting for whatever you're building with Python.
- 1:09:14Get four months free at talkpython.fm/Linode.
- 1:09:17That's L-I-N-O-D-E.
- 1:09:19With TopTal, you get quality talent without the whole hiring process.
- 1:09:24Start 80% closer to success by working with TopTal.
- 1:09:28Just visit talkpython.fm/TopTal to get started.
- 1:09:32That's T-O-P-T-A-L.
- 1:09:34Want to level up your Python?
- 1:09:36If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 1:09:41Or if you're looking for something more advanced, check out our new async course that digs into all the different types of async programming you can do in Python.
- 1:09:49And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 1:09:53It's like a subscription that never expires.
- 1:09:55Be sure to subscribe to the show.
- 1:09:58Open your favorite podcatcher and search for Python.
- 1:10:00We should be right at the top.
- 1:10:01You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:10:11This is your host, Michael Kennedy.
- 1:10:12Thanks so much for listening.
- 1:10:14I really appreciate it.
- 1:10:15Now get out there and write some Python code.
- 1:10:17I'll see you next time.