Skip to content
PodSubs
ShowsPeopleGuestsJobsNewsEvents
More
BlogPodCred
Menu
ShowsPeopleGuestsJobsNewsEventsBlogPodCredNetworksSign in
Join nowSign in
  • Shows
  • People
  • Jobs
  • News
  • More
    • Guests
    • Events
    • Blog
    • PodCred
    • Networks
    Join nowSign in

The Python Show

The Python Show artwork

49 - EdgeDB and Python with Yury Selivanov

Oct 30, 202450 minAudio
ShareXLinkedInFacebookWhatsAppEmail

In this episode of The Python Show Podcast, we welcome Yury Selivanov as our guest. Yury is a core CPython developer and co-founder of EdgeDB and MagicStack. We chatted about many different topics, including the following: * Core Python development * EdgeDB and how it differs from relational databases * Python without the GIL * Python subinterpreters * Memhive * and more! Links Learn more about our guest and the topics we talked about with the following links: * Yury’s GitHub page * EdgeDB on GitHub * EdgeDB’s website * PyCon 2024 - Yury Selivanov: Overcoming GIL with subinterpreters and immutability * An article about Memhive Get full access to The Python Show at www.pythonshow.com/subscribe

Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.

Transcript

From the show. Tap a time to play from there.

SPEAKER_03Welcome to a brand new episode.

SPEAKER_00Mike Driscoll, The Python Show.

SPEAKER_03Hello and welcome to The Python Show. I'm your host, Mike Driscoll. And today I'm honored to have my friend Yuri on the show. Yuri is a core CPython developer and he's also the CEO and co-founder of EdgeDB. Welcome to the show, Yuri.

SPEAKER_01Thank you for having me.

SPEAKER_03It's good to have you. So anyway, I like to start off the show by kicking us off and just having you tell us a little bit about yourself and perhaps your journey into programming.

SPEAKER_01Um, no, that's, that's, that's a deep question. Um, uh, my journey started relatively early in high school. My first programming language was, uh, Turbo Pascal, uh, very, uh, uh, soon followed by, uh, a lot of C, uh, and then I was kind of oscillating between the two. Uh, at some point of time I picked up PHP and, uh, When I immigrated from Russia to Canada in 2004,

SPEAKER_00I believe,

SPEAKER_01maybe slightly earlier than that, my first job was actually related to PHP a lot. At that job, I met my co-founder. Then we decided to start our software consultancy. And then we decided that enough of PHP for us, let's pick something fresh. And that fresh was Python. So this was roughly the road.

SPEAKER_03So that would have been early in Python 2.0, 2.3, somewhere in there?

SPEAKER_01I think Python 2.6 was out. And we tried using it for some time. And then we discovered that we actually want to push the limits of what was possible slightly in Python and web frameworks and decided to create our own And so we started using Python 3, which was at alpha 2 or alpha 3, incredibly early, incredibly rough.

SPEAKER_01But we just decided, okay, so we are building this new thing for ourselves. We might as well just use the new programming language or new version of the programming language. And to be quite honest, in my brief exposure to Python 2, I wasn't able to build a consistent understanding of how to write Python code without having Unicode error exceptions and things like that. Not having the strict separation between Unicode and bytes

SPEAKER_01was hurting my brain. So even though Python 3 was so early and so many packages were not ported and it was a complete mess at the time, just this fact alone was sort of motivating me to just stick with it.

SPEAKER_03That's cool. You know, a lot of people like to complain about that whole breaking change in Python 3, but It hasn't bothered me at all in switching to it.

SPEAKER_01I think absolutely everybody complains about the migration from Python 2 to Python 3. Obviously, it was painful. Obviously, it wasn't automated well enough. Obviously, I think it spiraled out of control a couple of times, and everybody was worried for the future of Python. That said, I think that everybody who I know, certainly all core developers, they all believe,

SPEAKER_01I think, strongly that ultimately the change was good. Like the actual proper separation of strings and bytes, making sure that we don't have strings, that everything is Unicode and things like that. I think that was the right move. Maybe there would be some strategy to make that move easier for the community. I don't know. It's not clear even now what that strategy would be.

SPEAKER_01But all in all, I think Python is on a good track now.

SPEAKER_03So did you get involved as a Python core developer in those early Python 3 days, or did you join much later on?

SPEAKER_01So I think my first contribution to Python was around 2011 or maybe early 2012. And that was basically me scratching my own itch. We were creating basically a data layer in Python, which later, years later, morphed into what HTTP is now. But back then, basically, we were just dabbling at that. And the data layer that we had...

SPEAKER_01It was basically an ORM, like a very early version of an ORM. In an ORM, you have all those dynamic attributes and dynamic accesses. And there is this little built-in in Python called hasAutor. And we're using it to check if this object has an attribute or it doesn't have an attribute. And back then, hasAutor was masking all of the exceptions that were happening in it. So basically, if a runtime error happened or something like that in your custom dunder getAutor hook,

SPEAKER_01hasAutor would just happily mask it. which obviously is the wrong behavior. This was the behavior of Python 2 and still is. But in Python 3, that was literally my first interaction on Python core mailing list. I asked the question why. I did some research before that. I knew that it was discussed before that email a number of times. But basically, I tried to make my case that this has to stop. This is extremely inconvenient. Maybe it should be fixed for Python 3.

SPEAKER_01I think it was Python 3.1 at that point or something like that. And yeah, I wrote two-line patch. And that was my first contribution to C Python. And I was kind of hooked reading the mailing list and sort of being aware what's going on. And that awareness ultimately helped me to become a core developer.

SPEAKER_03That's awesome. That's a cool origin story about becoming a Python core developer. That's really cool.

SPEAKER_01It always starts like this, yes, by scratching your own itch. And this is basically what I want to encourage all the listeners, is that you should always try to contribute if you can.

SPEAKER_03Yeah, you definitely should. And something I'll mention is you don't necessarily even have to contribute to the core, the code base. You could just be contributing to the documentation, because that's also really helpful to many, many people.

SPEAKER_01Absolutely. Maybe it's even more helpful. Documentation is a must, and documentation is something that you... You just cannot have a perfect documentation. I think you can have close to perfect interpreter implementation or something like that. But documentation, it's never enough. There is always something to fix. Yes. And in general, it's not so much about fixing typos. Like if a pull request to C Python is open just to fix a little typo, I mean, it can be rejected theoretically.

SPEAKER_01I've seen some pull requests that try to fix 20 typos and then core developers. We don't want to merge this kind of pull request because they introduce a lot of churn and sometimes those typos aren't even real typos, just some little things that people wanted to nitpick on. But if it's a recipe, if it's an important clarification to the documentation, Those things are incredibly important, and they can be definitely something to sort of get you into the contributing mode.

SPEAKER_03Yeah, and there's plenty of spots in the Python documentation that doesn't have a lot of good examples, necessarily. It explains the API, but they don't necessarily have working examples. So that'd be a good place to contribute, theoretically.

SPEAKER_01Absolutely. Absolutely, I agree.

SPEAKER_03So you've mentioned that EdgeDB was in the back of your mind when you started working on Python. Could you tell us a little bit about what EdgeDB is?

SPEAKER_01Oh yeah, absolutely. So it was on the back of our mind because we always, my co-founder and I, we were always dissatisfied with not just Python orms, object-relational mappers such as SQL, Alchemy or Django, but with them in general in every programming language. Basically, the root of the problem is that relational databases are absolutely everywhere around us.

SPEAKER_01But they were mostly designed in the 70s. And programming languages have seen a lot of progress since then. We have all those new, beautiful programming languages. Python is one of them, for what it's worth. uh whereas uh relational databases are just uh squarely stuck in in in the 70s and there is this huge gap uh between uh sql and between uh the data model uh being tables and the joints and your high level programming language like python uh or

SPEAKER_01javascript or uh c plus plus or something like that um we as an as engineers we just don't think in tables And ORMs try to bridge this gap. This is what they do. The main problem with them is that as abstractions, they leak inevitably. So it's only a matter of time until you try to express some query in an ORM and you just cannot. So you have to drop to a better SQL. And then suddenly all of this beautiful facade that ORM gives you,

SPEAKER_01all of this functionality just goes away out of the picture. There's one problem. The other problem is performance because unless you really is an expert in a particular ORM, And by an expert, I mean like really know how it's implemented inside out. You have no idea how many requests it will actually make. And for some innocently looking use cases, it can make tens or even hundreds of requests behind your back. And then your performance just goes out of the window and you don't know why.

SPEAKER_01And often when you need to refactor it, you will be to hell with this. I'll just write SQL. Again, abstraction just broke. Also, back then, schema migrations were an incredible pain. They still are because they're solved on the side, essentially. Relational databases have a schema, but nobody wants to deal with that schema at all. We just trust the database to enforce it, but we prefer to manage the schema with outside tooling. And basically,

SPEAKER_01my co-founder and I, relatively early, we started asking all those questions like, why is this the state of the things? Or is it the physical law of the universe that this must be how it is? What's going on here? And then the idea slowly materialized. I think around 2015 was the first time when we thought about this. Now, hey, we wouldn't have any of those problems if the database itself was more high-level.

SPEAKER_01What if instead of tables, you would have high-level object types? Instead of joins, you would just have attribute traversal, essentially. What if instead of SQL, which fundamentally operates on tables, we would have a query language that's spiritually close to SQL but operates on object hierarchies and graphs? that would make this database system incredibly close to your programming language. And it would actually make it easier for you to think and reason about this

SPEAKER_01database system as its user or as an engineer. Why do we trust external tools to manage the schema? When the database can actually have a high-level, beautiful language to define that schema and can have built-in tooling to help you with schema migrations, wouldn't that be nice? Because if that's the case, it doesn't matter what language you use with the database. The tools are always the same. Or if you are... I don't know, working on a big project where you have parts of it written in Go and parts of it

SPEAKER_01written in Python and parts of it written in TypeScript. It's nice when everybody agrees what the data model is, what's the source of truth in this layout, who controls what, what tools you use and whatnot. It actually removes a lot of friction. And basically that started HDB. And this is why we are still passionate about it, because I think that we are the only database startup that actually challenges the

SPEAKER_01status quo. Because if you look at database startups, most of them are... And I'm not... I'm going to say word just, what they do is actually incredibly hard and challenging, but still they're just bringing Postgres to cloud or MySQL to cloud, which is important. And it's obviously a tough thing to do correctly. But no one of them says, hey, can we actually try and do something better than SQL or try and elevate the data model?

SPEAKER_01As far as I know, HDB is the only company that specifically targets relational database, specifically targets PostgreSQL as the sort of like base layer and the underlying machine that drives it. We are the only company that tries to challenge the status quo and basically ask this question, hey, can we actually have a relational database that engineers will love building on top?

SPEAKER_03That's cool. So is that kind of your pitch for an engineer or for like a company to switch for? I mean, how would you get engineers really interested in doing this?

SPEAKER_01So it is definitely a pitch for engineers. If you look at the world of databases, I think there are two different strategies of selling them. One is you're making something very specialized, something that big companies or companies with deep pockets want. Let's say you're building a database specifically engineered for high frequency trading, then you just go to Wall Street and sell it and they will buy it if it's actually faster.

SPEAKER_01They will shower you with cash and they will buy it. Or you're building something for handling petabytes of data and analyzing them and whatnot. This is what Snowflake did, for example. Or, on the other hand, you build something that resonates with engineers. And this is what MongoDB did, famously. Because Fortune 500s, they didn't need MongoDB. They do now. But... Oh my God, like 15 years ago or 13 years ago, they didn't.

SPEAKER_01MongoDB is kind of old now. They didn't. That push was bottom-up push, meaning that engineers wanted to have a breath of fresh air and try something new. And it resonated with them. So with HDB and our ambitious goal of elevating the data model of relational database and elevating the query language, basically evolving SQL into something new. Sometimes I call our query language, HQL, I call it SQL++.

SPEAKER_01Like we're really trying to be sort of spiritual successors of SQL. You cannot sell this to Fortune 500s. They will not be buying it because they don't have good enough motivation for it yet. Hiring people who know this new emerging technology is going to be painful. Trusting this technology until you're a Fortune 500 company yourself is kind of tough for them. So it's possible to sell to them, but it's not a reliable strategy.

SPEAKER_01And while you can make a sale or two, it's unlikely that you will be able to build a thriving database business on top. So a better strategy in our situation and for other databases like MongoDB or Frankly, any infrastructure DevTools company that builds something really challenging and something that is really like a step function improvement over the status quo, the right strategy is bottoms up.

SPEAKER_01So essentially, get engineers to trust you and love you. And what we... specifically pitched with HTTP to engineers is that, hey, first of all, the data model is very high level. In fact, specifically, this is a Python-related podcast, HTTP data model was heavily inspired by Python. For example, we have multiple inheritances Well, in the context of HDB, it's not as dangerous as in Python.

SPEAKER_01In HDB, it actually enables better composability of your types. It's closer to mixed and abstract types. It lets you modularly define your schema and your types. So we have that with some advanced features like access control, which is more advanced than what you can do with Postgres with role-level security. We have our query language, HQL, which allows you to work easily with hierarchies,

SPEAKER_01and hierarchies is what we need to work with. Imagine that you are rendering a user profile page, and on that page you need to render information about the user. potentially some of their friends, maybe some of the recent blog posts that they've made or recent comments on the system. And this is a hierarchy. And in HDB, it's trivial to express these queries. Literally, you will be able to do this 10 minutes in. To fetch all this data at once in one go, in one network request, and in one query, it's trivial.

SPEAKER_01So some developers like that. We will soon, in a matter of a few weeks, in December 2024, release a new version of HDB, and we will allow actually SQL as well, in addition to our query language. So you will be able to combine, let's say, SQL alchemy with our Query Language Edge scale if you want to take advantage of both the underlying Postgres that we have and the advanced data model that we built on top. Sometimes it's about mundane things like installation.

SPEAKER_01So if you want to install five different versions of Postgres or just even two, how do you even do this? With Homebrew, it's challenging to install multiple versions of multiple things in parallel. With Mac ports, if anybody uses them, it's somewhat easier, but then you have to sort of use version command, like you have to append... um like the exact version uh to the command name and stuff like that if you are

SPEAKER_01manually installing them then good luck or if you're using a dmg installer or uh on windows you just install uh uh postgres you're stuck with that particular version With HDB, you can have as many as you want. You can have hundreds of different versions of HDB. The system just automatically shuts them down when they're not in use. You can install any version at any time. Everything is zero configurable. You don't need to mess with host names and ports. Everything is automatic and just works. You don't need to do any configuration.

SPEAKER_01You type one command, you log into the cloud and you create a cloud instance. You can sync your data to the cloud instance. All of that is frictionless. So basically what I'm saying is that with EdgeDB, we're looking at this problem of developer experience with the relational databases from all the different angles, both from the angle of the data model, from the angle of the query language, from the angle of how do you develop locally? Is it convenient or not? From the angle of cloud, from the angle of UI, we have an amazing UI, and I think it's very important.

SPEAKER_01A lot of our users, basically, they don't have to create an admin panel because they can just use the built-in UI. So things like that. And believe it or not, I just scratched the tip of the iceberg. HDB is a huge platform. But ultimately, yes, developer experience matters. I think that hardware is cheap. People are expensive. And if you want to move fast, it wouldn't help you if you have a data center worth of compute and not enough

SPEAKER_01engineers to build what you want. So if you want to build fast, you need to choose the proper stack. This is why Python is popular. This is why JavaScript is popular, because with those languages, it's fast. You can be incredibly productive. And EdgeDB, I think, is the missing piece of the puzzle, because it's the only database that sort of tries to optimize for the modern age, for the modern application development.

SPEAKER_03That's really cool. I'm curious, is that cloud only or if you were like an enterprise and you wanted to host it yourself, could you do that?

SPEAKER_01Oh, absolutely. We are fully open source. HDB is Apache 2 license. And again, it runs on top of PostgreSQL, which is a very important implementation detail. As a user, you don't need to worry about it. You don't need to care. HDB will manage PostgreSQL automatically, install it automatically. and whatnot. But it is what gives a lot of our users peace of mind because they know that it's built on top of a very proven, solid technology.

SPEAKER_01That said, we do allow you to deploy GDB yourself. We have a Docker container if you want. If you don't want to deploy it to our cloud, You can deploy it on top of AWS Aurora or on top of Google Cloud Postgres. You can do any of that. We used to have a custom patched Postgres, but in time, basically, we upstreamed most of our changes to the official Postgres distribution. So now we can just enjoy running on top of vanilla Postgres.

SPEAKER_03I see. Yeah, I only asked because I've worked for multiple companies that like to host a lot of things themselves, not necessarily in the cloud. Yes. Yes. So what's new? I know EdgeDB had a big release this year. Is there anything new you'd like to highlight or something new that's coming soon? Anything like that?

SPEAKER_01I can highlight a couple of things, I guess, from the previous release, but I think I do want to mention the next release because I think the next release is going to be where a lot of people will be able to start using EdgeDB. asking way fewer questions than they usually do. So with the previous release, what we shipped is an AI extension, which is actually pretty cool because it allows you to just annotate some of your

SPEAKER_01database types with a so-called deferred index. You create deferred AI index, you configure the system. Hey, I want to use OpenAI or I want to use Anthropic or I want to use Mistral. and then hdb will automatically create indexes for the types that you specified anytime you insert data delete data update data hdb will handle the whole embedding thing for you and you can basically create embeddings for absolutely anything you can provide arbitrary expression for example you because we're a hierarchical

SPEAKER_01database essentially What you can do, you can even write a complicated query for the index forming a big JSON payload, and you can create embeddings for that, for example,

SPEAKER_00easily,

SPEAKER_01without any code written, just in your schema. And then HTTP will give you RAG endpoint. It will give you this Fusion Search HTTP endpoint that you can use. You have APIs in Python and JavaScript to access those endpoints. And then in the admin UI, once you annotate your schema, with those little AI deferred indexes, you can chat with your database literally using GPT. Again, if you want to, this is all an option.

SPEAKER_01Shipped a bunch of improvements to the built-in auth module because I think that auth is such a pain absolutely for everyone who starts building a new application. Just the thought of implementing email password authentication or implementing OAuth for something hurts me personally. Yeah. So with HDB, it's just built in and it's integrated with access policies and everything else. And we even give you built-in UI. So if you don't want to focus on implementing the login form or remember password form,

SPEAKER_01the database gives you absolutely everything. When you don't need it, you build your custom. But ultimately, it's something like both auth and AI extension allow you to build an application literally overnight. A functional, proper application with proper access control, security, AI, and whatnot. That's cool. Anyway, the new upcoming release will feature one big thing, which is SQL support. I already mentioned it. Which means that... you will be able to basically use sql alchemy and our an r query language hkl side

SPEAKER_01by side in the same project you will be able to slowly migrate your existing project to hdbe if you want to and vice versa for example you started with hdb and for some reason you discovered that hey like this is not for me i want vanilla postgres or maybe you have concerns about us being relatively a small company we're definitely not fortune 500 yet so uh if you have any of those concerns this basically provides an escape hatch because uh with that you can migrate away from uh from from us to vanilla postgres basically at any time you

SPEAKER_01want uh um you you can definitely uh uh conceive a process uh on how on how to do that And on top of that, we will also add support of GIS types by wrapping the post-JS extension, which is important for a lot of people who have to have any sort of geographical functionality in their application. But also, we will have language server protocol finally shipped.

SPEAKER_01So that means linting of your schema files in your, let's say, VS Code. go to definition, all of those things should work. There are a bunch of other changes and updates to HTTP. It's a huge release for us, but I would say that SQL compatibility is definitely the highlight of this release because of how many people will be able to start using HTTP.

SPEAKER_01And a quick note is that the SQL compatibility wasn't exactly easy to add. Even though we're running on top of PostgreSQL, we cannot just bypass SQL commands straight to Postgres. We basically had to implement a compiler from SQL to SQL because HDB's data model supports some of the features that Postgres doesn't. and we needed to sort of properly do it. Interesting.

SPEAKER_01It's going to be a very powerful database, even if you don't want to use HQL, our query language. If you just use SQL, you're going to have more capabilities than what you usually can get out of Postgres in a SQL system.

SPEAKER_03That's amazing. That sounds really cool.

SPEAKER_01Thank you.

SPEAKER_03I think we'll pivot back to your CPython stuff, because you'd mentioned that you could tell us what to expect with the new Python without the global interpreter lock.

SPEAKER_01I'm not an expert on that, but what I can say is that I'm personally excited about this a lot. I think this must be the future of Python, where we don't have the GIL. With It's an extremely complicated project, though. And the code base becomes extremely complicated as well as a side effect of that. On top of that, there is a team now, a PyPerformance team that actually implements JIT in Python.

SPEAKER_01So suddenly we have two extremely complex projects running side by side. And obviously it will take time to stabilize both. uh to to actually see start seeing real benefits from this jet work that's been going on for a couple of years now at least uh and also from no gil even though uh you can actually now try python without the kill uh it will likely be a couple of

SPEAKER_01years until we can say this is stable enough And it will be way, way longer than that until you can actually reliably tell if some library on PyPI is thread-safe or not thread-safe. That's a whole separate conversation because a lot of assumptions were sort of implicitly designed in the many Python packages. So that's a separate conversation. Not a lot of people used multithreading in Python before.

SPEAKER_01But no, obviously way more people will, and they will suddenly discover that a lot of things are not that safe.

SPEAKER_02Yeah.

SPEAKER_01So on that front, if you want, I can cover another subject that I know slightly more about and which people can play with now, which is subinterpreters.

SPEAKER_03Yeah, that's a great topic. I'd like to know more about that myself.

SPEAKER_01All right, so Eric Snow has been grinding for multiple years the subinterpreter support in Python, which was a thing since pretty much forever, but not too many people actually used it seriously. And ultimately, subinterpreters, in my opinion, make only sense when you can actually have each of them have their own guild. So suddenly you can be in a situation where you have an OS process that runs

SPEAKER_01multiple CPython interpreters side by side, completely isolated, sharing nothing, not even the global interpreter log. So that basically gives you parallelism and basically enables Python for multi-core. Just like NoGil, but with a slightly different sort of approach.

SPEAKER_02Mm-hmm.

SPEAKER_01Now, why would you use subinterpreters instead of multiple processes? That doesn't have a clear answer, to be honest, even now. There are some good answers for that, but nothing that's sort of production-ready, ready to use, something motivated that would let people to just go and start using subinterpreters, even though since Python 3.12,

SPEAKER_01you can have multiple of them, each holding their own GIL running in parallel. So I think I might have an idea on what we can do with this stuff. And I started working actively on it in May before the PyCon US conference, where I had a talk on this subject. And then I got slightly distracted and paused my work. But I do want to get back to it. So what I built, essentially, is a thing that I called MemHive.

SPEAKER_01And MemHive allows you to efficiently share data between those subinterpreters. Which is, if you think about it, this is the holy grail, because as soon as you have something running completely concurrently, and you can share two workers, two Python workers, each of them in its own subinterpreter, each of them potentially on its own core on the CPU, As soon as you can efficiently share data between them, it means that, boom, you can actually start building real-world practical applications and benefit from this.

SPEAKER_01Because sharing data is the most expensive thing when you're building a multi-process application because you have to somehow send the data. If you pickle it, that's slow. And on top of that, you also have the communication overhead as well. And then you have to unpickle it. And what if you have a lot of data? What if you want to implement something low latency? Basically, all these use cases almost make Python not suitable for any of that.

SPEAKER_01And I guess the dream for Python would be to have something like m-to-n scheduling. It's like when you have m asyncio, when you have a few asyncio interpreters that are just running asyncio event loops, running in the same process efficiently, exchanging data between them, and efficiently sort of being able to schedule tasks in each other.

SPEAKER_01As soon as you can build that, something that is called basically m2n scheduling, you will be able to scale your Python code to as many courses as you'd like and basically achieve almost linear scalability, which is obviously the dream. So the way MemHive achieves this is by using a concept called immutability. So immutability is a relatively undercover thing in Python, I would say.

SPEAKER_01We do have some immutable types in Python. Strings is one of them. You cannot mutate a string. You can create a new string from an existing string. You cannot just replace nth character of the string. So tuples is another example, a small record type where you can have a few elements and it's also immutable. Name tuples as a derivative of that. What else? Bytes, I guess, integers.

SPEAKER_01You cannot mutate an integer. You can create a new one if you want out of a sum or a multiplication of another ones. So we do have some immutable types, but it's not common for a Python engineer to actually use immutable data structures that much. So this is a relatively new thing. But there are languages that heavily rely on immutable types. Functional languages typically take advantage of them. And many believe that if you use immutability properly,

SPEAKER_01it actually leads to just more reasonable software in general. Because if you think about it, whatever you pass to your function will not be mutated ever. The function can return something new, essentially. But As soon as you don't have mutations, you can reason about the data flow in your program easier. But then if you start thinking about things like multi-threaded programming, then immutability becomes even more crucial because in multi-threaded programming,

SPEAKER_01This is what you want to avoid specifically. You want to have as little shared state as possible. And if your application is built using a mutable data structure, that makes it easier for you to identify where that mutable state is, which means that it's going to be much, much easier for you to debug your program. And I believe that once Python actually gains no-guile capability, a lot of people will start looking towards immutable types.

SPEAKER_01So... Anyway, my own journey with immutables started with PEP, I think it's 550 or 555, one of them, which is a context vars module for a standard library. My idea was to add a thread local like thing for async.io. So for OS threads, you have this magical, almost magical object in Python where you can store a value in it,

SPEAKER_01and that value will only be visible by the thread that stored it. And multiple threads can store value in this object, and each of them will only see their own value. So we needed an equivalent for this for async.io. and uh uh in order to build it uh that that was actually quite quite a challenge probably the most challenging thing that i've ever done because i actually needed to burst through my own bubble and actually go and learn about functional languages and actually understand how mutability works and how some of the data structures

SPEAKER_01that i had to implement for this work but ultimately the the requirement for async i was that this wouldn't include the support for this feature wouldn't uh impure any overhead at all on asyncio. And immutable data structures was one of the ways how I could do this. So I researched how specifically Clojure language, it's a language that runs on GVM, how they implement their immutable dictionaries. Because if you think about it, it's actually magical.

SPEAKER_01In Clojure, you can have a dictionary with a billion items. And then you can create a new dictionary out of the previous one with one different key, for example. And that operation is basically all one. Like it's insanely quick. This is pure magic to me. And I had no idea how this is implemented because the most straightforward way is to just copy the dictionary element by element and replace one of the elements with the new one. And that's obviously slow. So how did they do it?

SPEAKER_01So anyway, I researched that. The answer is that there is this exotic data structure called HAMT, hashed array map try, which is basically a huge tree behind every dictionary. And that tree allows you, the structure and the layout of the tree allows you for the super-fast operations. Anyway, back to MemHive. So I had this experience with immutable data types, with implementing those data structures myself in C.

SPEAKER_01And then it struck me that there is one interesting thing about subinterpreters that multiple processes don't have. And the interesting thing is that they all run in the same memory space. That ultimately, if you allocate something in C, for example, and pass a pointer to it from one submitter to another, the other can just read it without any overhead. And this got me thinking that, hey, those things run in parallel.

SPEAKER_01Each of them has its own GIL. But they all share the same memory space. So if we have our types in our pure Python code, if my type is immutable, if my value is immutable, then maybe I can actually safely share it. Just share the memory of it to another sub-interpreter. Because I know it's immutable, I will not mutate it. The only thing that I need to make sure of is that this value that I'm sharing will not be garbage collected.

SPEAKER_01And that was the missing piece of the puzzle, at least for me, because I already knew how to implement complex immutable types like dictionaries, and I know how to implement things like lists and whatnot. And this is how the idea of MemHive was born. What if we can have this little library that allows you to spin up CPython interpreters inside the same process the library can provide you a queue primitive so that you can send data between them.

SPEAKER_01And if the queues can only send immutable data between subinterpreters, we can basically ensure that this will always work and that the other subinterpreter will not need to copy data. It can just look inside the data and basically on the fly give you reflected objects. So basically this completely solves the serialization problem and pickling problem. You don't need to pickle,

SPEAKER_01you don't need to serialize, but you do have to use immutable data structures specifically sort of optimized for this. But maybe this is a relatively low price to pay. And maybe if, for example, I finish MemHive and one way or another it becomes popular, Then when no guilt happens, we just switch it from using subinterpreters to using threads. Because I fundamentally believe that immutable data types are anyway the best

SPEAKER_01practice for multi-threaded programming. Same for queues. For example, you should use queues in your multi-threaded code to make sense of it. Maybe this is a good idea. So anyway, this is what I'm passionate about. I had an early benchmark, and as predicted, Python does scale nearly linearly with this approach to multiple cores. The only missing piece is essentially finish the implementation of MemHive, which I'm fairly...

SPEAKER_01There is a lot that's already done, but it is at the proof of concept stage. But I still do need to find some time, maybe a couple of months, and finish it off. And after that, it should be possible to build simple applications that just get the full power of your CPU.

SPEAKER_03That sounds exciting. I'd like to try that.

SPEAKER_01And again, I'm speaking openly about it because I do think that the approach is valid and I wouldn't mind if somebody else in the community gets inspired and implements this. The approach will definitely work. I'm 100% sure of that. But obviously, yes, it does require a lot of low-level work. I think even at the proof-of-concept stage, memhive is about 10,000 lines of C, which is a lot of C code. Yeah.

SPEAKER_03Yeah, it's a lot. Wow. So is there anything else you'd like to tell us about that you've been working on, or...

SPEAKER_01I guess not. Not really. I am excited about Python. I am excited about Nogil. I'm excited about the JIT because I do believe that very soon Python will start getting faster and faster and faster. it's uh it took us a lot of time many years until we get to this point but exceptionally smart people are working on this now and and they're making progress

SPEAKER_01so uh i'm faithful If any of the listeners are building Python applications, fast API applications, or anything that needs a database, I encourage you to take a look at EdgeDB, because I do believe this is as close to a Pythonic database as possible. At least when most of the system was designed, I basically used, me and my co-founder basically used Python Xen

SPEAKER_01to decide what feature must be there, what feature can wait, and what not. So I think Python shaped it a lot. It is a Pythonic database. So I encourage you to take a look at it. And I also encourage you to contribute to open source projects. Contributing to open source gets me to where I am. I'm immensely grateful for all the time and all the people that I met. doing that. If Python is something that you're passionate about and you have a niche,

SPEAKER_01you don't like some documentation or some functionality is missing, find a way to contribute. It's never been easier. If you like TypeScript and something doesn't work there the way you like, go contribute to TypeScript. The most important thing is that you build this muscle of contributing to open source. And if you don't want to use it later, don't. But chances are the community will benefit from your work and you yourself will benefit from it in some very unexpected ways.

SPEAKER_03I totally agree. Giving back is also rewarding personally as well as professionally. Exactly. When you contribute to your open source, there's a record of your contributions that you can point to And show other people, like a potential employer might care that you've done that and you can show that you know what you're talking about. 100%.

SPEAKER_01All of those contributions will help you with getting better jobs, starting your own business, maybe getting funded. For your ideas, open source profile is the number one thing that I personally look at. And a lot of my friends who are running businesses or working for big businesses, they all say that the first thing they look usually is the open source profile. Obviously, it's okay if you don't have one. But if you do, it's just a bonus.

SPEAKER_01It's a very nice bonus. And it will help.

SPEAKER_03That's good to know. And I think that kind of brings me to the end of my questions for you. So I just want to thank you so much for being on the show today, Yuri.

SPEAKER_01Thank you for inviting me. I enjoyed it thoroughly.

SPEAKER_03Yeah, thank you. And for anyone who'd like to know more about what Yuri's been talking about here on the show, I'm going to include links to HDB and MemHive. And I'll try to find some articles on Python without the GIL that I can put on there too, so people can read about that.

SPEAKER_01And there is a very nice article on LWN about MemHive, which I'll share a link with you. It's a good read.

SPEAKER_03Cool. I'll pop all those in there and anything else I can think of that's related to our talk. All right. Thanks again for listening.

Open the transcript file

Hosts and Guests

Nobody is credited on this episode yet.

Credit the people on this episode

Be the first to rate this episode

More episodes

All episodes
  1. 57 - Python Developer Tooling Handbook with Tim Hopper

    Aug 27, 202643 minAudio

    In this episode of the Python Show Podcast, I am pleased to have Tim Hopper as my guest. Tim is the author of the Python Developer Tooling Handbook, a free online resource for everyone. We chat about Python tooling,...

    Credit the people on this episode

  2. 56 - Python Illustrated

    Mar 13, 202653 minAudio

    In this episode, we hear from two sisters who put together a beginner's book about Python. The unique hook for their book is that one sister wrote the text while the other did the illustrations. Listen in as we learn...

    Credit the people on this episode

  3. 55 - The Python Documentary with Paul Everitt

    Sep 3, 202540 minAudio

    In this episode, we have special guest Paul Everitt on the show to talk about the new Python Documentary that came our last week. Paul is the head of developer advocacy at JetBrains and a “Python oldster”. We chat about...

    Credit the people on this episode

  4. 54 - Neural Networks and Data Visualization with Nicolas Rougier

    Aug 27, 202537 minAudio

    In this episode, we have the honor of having Nicolas Rougier on our show. Nicolas is a researcher and team leader at the Institute of Neurodegenerative Diseases (Bordeaux, France). We discuss how Nicolas utilizes...

    Credit the people on this episode

  5. Writing Creating TUI Applications with Textual and Python

    Jul 24, 202525 minAudio

    In this episode, Mike Driscoll talks about his latest book, Creating TUI Applications with Textual and Python. Learn about how and why the book came about, what were some pain points in writing the book and how much fun...

    Credit the people on this episode