- Podcasts
- Talk Python To Me
- #568: Cloudflare Python Workers
About this episodeWhere does your Python code actually run? A server in Virginia? A container in Frankfurt? Pyodide put Python into the browser. Cloudflare…
Episode sponsors
Talk Python Membership
Talk Python Membership
Talk Python Courses
Links from the show Guests
Gyeongjae Choi: linkedin.com
Hood Chatham
Dominik Picheta
Pyodide: pyodide.com
Python Everywhere: The State of Python on WebAssembly - Hood Chatham, Gyeongjae Choi: www.youtube.com
PEP 783: peps.python.org
D1 Databases: www.cloudflare.com
Hyperdrive: www.cloudflare.com
R2 Object Storage: www.cloudflare.com
Queues: developers.cloudflare.com
AI: www.cloudflare.com
now generally available: blog.cloudflare.com
Memory Snapshots for CPython (Python Language Summit 2026\): blog.python.org
Cloudflare Worker Limits: developers.cloudflare.com
Litestream: litestream.io
workerd: github.com
Are we PEP 783 yet?: pyodide.github.io
The Birth & Death of JavaScript: www.destroyallsoftware.com
Watch this episode on YouTube: youtube.com
Episode #568 deep-dive: talkpython.fm/568
Episode transcripts: talkpython.fm
Theme Song: Developer Rap
🥁 Served in a Flask 🎸: talkpython.fm/flasksong
---== Don't be a stranger ==---
YouTube: youtube.com/@talkpython
Bluesky: @talkpython.fm
Mastodon: @talkpython@fosstodon.org
X.com: @talkpython
Michael on Bluesky: @mkennedy.codes
Michael on Mastodon: @mkennedy@fosstodon.org
Michael on X.com: @mkennedy
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#568: Cloudflare Python Workers
Where does your Python code actually run? A server in Virginia? A container in Frankfurt? Pyodide put Python in the browser. Cloudflare put the browser into the server. Combine the two and you get Python workers, now generally available from Cloudflare. My guests are Hood Chatham, Jay Choi, and Dominic Pichetta from Cloudflare. Hood and Jay maintain Pyodide. Dominic got Python running in Cloudflare workers. Your FastAPI app ships as plain files. Then you can run it on CPython, compiled to WebAssembly, inside V8, at over 350 locations worldwide. This is Talk Python To Me, episode 568, recorded October 9th, 2026. Welcome to Talk Python To Me, the number one Python podcast for developers and data scientists. This is your host, Michael Kennedy. I'm a PSF fellow who's been coding for over 25 years. Let's connect on social media. You'll find me and Talk Python on Mastodon, Bluesky, and X. The social links are all in your show notes. You can find over 10 years of past episodes at talkpython.fm. And if you want to be part of the show, you can join our recording live streams. That's right. We live stream the raw, uncut version of each episode on YouTube. Just visit talkpython.fm/youtube to see the schedule of upcoming events. Be sure to subscribe there and press the bell so you'll get notified anytime we're recording. Tomorrow, Wednesday, November 4th, we're having our first tech meetup, our live tech event as part of Talk Python membership. If you're not yet a member, well, join up and support the show and get an awesome community and experience to boot. Just visit talkpython.fm, click membership in the nav bar, and you'll be off to the races. If you are a member, we'll be talking about Talk Python in production and some really wild new ideas that I'm exploring. I think you will find both fascinating and actually really practical, but no one's doing it. So join me on Wednesday, November 4th. And if you're listening to this after and thinking, Michael, why did you just put this in here? Nobody can listen to it because most people listen to after. These tech meetups come with recordings. Just visit talkpython.fm, click on membership, and then browse past member meetups and you'll be able to get it. So hopefully either you can join live or you can join afterwards. Become a member, support the show, join the event. Dominic J. Hood, welcome to all of you to Talk Python To Me. Awesome to have you here.
Yeah, I'm really excited to talk about Python on the Edge. It's very exciting what you all have announced. And I know it's been out there on Cloudflare for a little bit, but it's now officially formalized, officially supported, a little more smooth. So Python workers, very, very cool topic. Before we get to that, though, let's just do a quick, who are you? Just give me a little bit of your background and what you do and so on. Dominic, we'll just go around the squares here. Dominic, go first.
Sure. I mean, I can get started how I got into Python. And that starts at, I guess, a very young age when I was playing with Visual Basic.net. And I realized that I hate Windows. I needed to get to Linux and Visual Basic.net back then did not support Linux at all. So a couple of my friends were using Python. So that's how I got into it. Afterwards, I actually got a little bit annoyed that Python was interpreted. So I looked for alternatives and I got involved in the NIMH programming language. And I became one of the core developers of that for a while. Fast forward a lot. I was working at meta and then I moved to Cloudflare to work on the workers runtime team working there on a bunch of features for workers in general. I actually was the person I guess that kind of experimented with Pyodide at Cloudflare and got it working and running and then eventually we got Hoodin to help us out with that and we started Python Workers and I've been working on that ever since as well And still do.
It is, it is. You know, I think it's interesting to contrast that with Python, actually, because Python also had many of the same goals of VB, but it somehow accomplished it with a lot less stuff on the screen. You know, like there's not begin if, end if, all this type of stuff. But yeah, very, very fun. Jay, welcome.
Hey, I think I can start with how I started using Python as well, like Dominic. So I also started programming using some Visual Basic NC when I was like 10 years ago. And then I started learning Python when I was in second grade in university, I think. I was having some data science course learning about packages like ThumbPy and SciPy. That was when I first started using Python. And I just fell in love with Python. Like making apps were so easy with Python. So I was using, I started, starting then I started using Python for like 10 years. And around 2021, I started contributing to a library called Pyodip, which I think we will talk a lot in this podcast. And yeah, after maintaining it for around one or five years, I got invited to Cloudflare to work on Python workers using Pyrodite from Hoots and joined Cloudflare. And yeah, after that, I was working on Cloudflare Python workers for around a year.
I started out on Lispy MLE languages. So I'm a mathematician by training. I wrote my first Python code for undergraduate introduction to programming class. but then I didn't use it at all until I had to write an email schedule for the math seminar which was in Python 2.7 and I am as a mathematician I was very interested in math software to try to get software that does a good combination of like research level you know like high performance computing and also just like quick experiments and lets people figures step out. And I found out that mathematicians don't like to install software on their computer. They assume it won't work. Of course, a lot of...
Yeah. And also, a lot of math software is made by mathematicians and it's not an easy thing to distribute software for other people to use. And so it's like they have something that works on their computer and they tell all their friends, oh, you should use this. And they say, nah, man. So that was how I got interested in WebAssembly. I mean, I first started by writing software in LaTeX because mathematicians already have LaTeX on their computer and they already have TechLive on their computer. And that's a package manager. It can just get your package onto their computer automatically. But like people just wouldn't use things that didn't come sort of connected with something they already had on their computer. And because browsers are a universal computing platform, you know, I watched the birth and death of JavaScript back in like 2014 or whatever.
Yeah, yeah, it's so good. And so I was very excited about the idea of like, oh, we should just make the software work in WebAssembly and ship it to the browser. And then mathematicians won't be able to like complain that it's maybe takes some work to install. But then if you want to have like, you know, powerful math software, you know, I wrote all the linear algebra stuff in Rust, but then, you know, you don't really get that much control over it from Rust. And you can make a GUI, you can give people JavaScript, But, you know, JavaScript is really a bit anemic in terms of, you know, like, for instance, our logic have lots of tables of things that were indexed by, you know, tuples of integers. And like, you know, having a map in Python that is nicely indexed by tuples of integers is like, you know, you could do it in JavaScript. It's just it's just a pain. So I really wanted to get Python to be in my web application with everything else. And so I found out about Pyodine and I started messing around with it, but it was really buggy and didn't work very well. you know, I mean, it was a sort of technical, you know, like demonstration. I mean, it showed a lot of promise because, you know, there was NumPy there running in the browser. So, you know, these binary extensions were working. But, you know, Mike Dropeboom did like amazing work at Mozilla to get that working. And I mean, Mozilla really created this whole cool chain on top of it. Anyways, I can maybe say more about that later. But I found PyDyde and it was just a bit after mozilla killed the whole project so it wasn't you know um roman urchuk was like just barely keeping the lights on and i showed up and i was like here are all the things that are bad about what's currently going on it kind of expecting him to be like oh like you're an asshole go away but he was like oh well why don't you come fix those things and so so i so he made me into a maintainer it actually first took another math that i was working with on the same project uh dexter who who had some experience with open source and so he started contributing to fix the problems that i found i I was like, oh, I guess I can contribute to this. Yeah.
Right. They weren't defensive at all. They were just like, great, we want this to be better. Like, if you want to make it better, please make it better. And so then I got a National Science Foundation postdoc for math. I said I would improve mathematical computing software. And so I ended up spending most of that postdoc working on Pyodide. And then when I was leaving the postdoc, I got hired to Anaconda to work on their PyScript stuff. And then about six months later, I got connected to Cloudflare and they were interested in me working on making Python workers exist.
Yeah, it was really an interesting project. Actually, a lot of people first learned about running Python in the browser from PyScript, not from Pyodide. Yeah, it was a really interesting project. It's very unfortunate they turned into a maintenance mode recently, but they did a real great job. I know, it makes me sad.
Maybe it's just taking a break. Maybe it'll come back in the future in some amazing form. I would love to see a React or a Vue alternative that just runs all on PyScript. But, you know, I don't think backed by Pyodide, backed by MicroPython, which would be, you know, I think a little more practical for web front end. But that is an alternate reality and a different thing that we're talking about. So let's begin maybe just a quick review of like running in the browser. So the goal of Pyodide is to run wherever WebAssembly runs. Primarily, that's the browser, though it doesn't have to be. You know, you can kind of treat WebAssembly a bit like a Docker type of thing, like an isolation. You can use the JavaScript. Right.
10 tends to a hundreds on any given continent right so the idea is with these these kind of getting ahead of ourself a little bit but the idea is like well what if we could take this pyodide thing that is a miniature python runtime and put it really really close to all the users and execute it there instead of routing back, you know, somewhere in US East 1 or something like that, right?
Yeah, precisely. I mean, you're giving the exact explanation that I think a lot of our, like, marketing people would give, which is great. Basically, yeah, like, you want as little latency as possible for your users, right? So you want to run Python code as close to them as possible, and this is a very great way to do that.
Yeah, and it doesn't run, to be clear, what we're talking about does not run in the browser of users like Pyodide can. It runs in the edge nodes of Cloudflare that happen to be very near the users, right? So I think that's a big difference because it gives you access to an entire platform like data and queues and all that kind of stuff that we're going to talk about, right?
The way I like to describe it is like Pyodide is kind of this crazy idea of like, we should put Python into the browser. And then Cloudflare is kind of this crazy idea of like, we should put the browser into the server. and so when you put Python into the browser and you put the browser in the server, that's a funny way of getting Python into the server.
Yeah. So let's talk PyDyde just for a minute before we dive in, now that people see where we're going with PyDyde. Let's talk about it a little bit because very recently, let's put some, one month ago, Hood and JU2 gave a talk at EuroPython, which I thought was really interesting. Python Everywhere, the state of Python on WebAssembly. So this is a more general, not just Cloudflare worker type of story, but give us an update. What is the state of Python on WebAssembly?
Things are good. It feels like there is less and less stuff that's going wrong. So I have these Python enhancement proposals to try and rearrange the governance of our code so that it can live maybe more of the stuff in the CPython repository. But yeah, I mean, I remember just a few years ago that there were always like crazy problems and lots of confusing bugs. And it was like, yeah, like we got lots of bug reports that would just be like, I have no idea what to do with this. But it feels like we really polished it a lot. And I mean, especially Jay moved all of the package build system into external repositories. And so the package build system is much less causing us trouble, although it's still still causing us some residual trouble. But now we have the PEP that says we can upload packages to PyPI. So it really feels like it's kind of making this graceful landing and approaching something that, you know, it will always take maintenance to keep up with upstream Python, but I'm sort of feeling like it's taking less and less in addition to that, which is nice.
What's the situation or the state with Pyodide and Python on WebAssembly in terms of being officially supported by the core developers of Python? I feel like it's gotten some sort of build support acknowledgement before they were willing to ship CPython that Python on WebAssembly also compiles. Is that right? Yeah,
so, right, so Python, sorry, so the platform we have is called Emscripten, right? This is the Emscripten compiler tool chain, which is this incredible thing that is front end for Clang and ships. JavaScript system calls JavaScript implementation of a file system. It's like a JavaScript Musa Linux that Emscripten compiles to.
Yeah, it's one of the greatest videos ever, for sure. And so, okay, so Emscripten is the platform, and Emscripten is tier three supported upstream by Python. And what that means is that we, well, we run it in CI, and, you know, it's best effort, you know, people are allowed to break it, and, like, I come back and fix it. And it's not a release block, or they, in fact, do not make releases for Emscripten. The reason they don't make releases for Emscripten is because they don't have really a version of like the OS module or for Emscripten. So they can't really do IO very much. They can't make requests. You know, they can't do HTTP stuff. They can't receive user input. And so in my opinion, if we build these executables, but no one would want to use it as it is built upstream. And so I have this proposal, which is PEP 818, which I just submitted a few days ago for review by the steering council, which is supposed to rectify the situation and make it so that we can have basically useful inscriptive iPhone binaries.
It's a little bit like CFFI or C types. You know, the package, the top level import is going to be JS types, which is following C types. And yeah, like the point of it is to allow calling JavaScript functions from Python, which is important because basically all functionality in a JavaScript runtime goes through JavaScript, just like the way all functionality in Linux goes through C.
But you also have, and this one is accepted, is this is a big deal, PEP783, the Emscripten packaging. I think packaging is having a big moment in the Python space. We just have the very first packaging council, right? That election that came out. And you talked about Jay doing a bunch of work to get the build of the packages other places.
Yeah, there's a long history about supporting packages in Pyodide and Python in the browser. And that 7.83, 7.83 unscrupulous packaging is really a lifesaver for us. Because so from the beginning of the Pyodide, we wanted to support packages like NumPy or SciPy or MatBotlib, all those scientific Python packages that needs to be cross-compiled to WebAssembly because they have C codes. So if the package only contains the Python code, we can just run it because there's nothing needs to compile. They're all interpreted. But if the package has a C or C++ or Rust code inside, they need to be compiled into a platform, which is for us WebAssembly or MScripten. So at the beginning and until 2025, we had to cross-compile and build all the packages in Pyodide CI to run them inside Pyodide. And I think last year we had around 300 packages that needs to be built in our repository. And it took like three or four hours to build all those packages in our CI. So even if you like make a little like one line change in Pyodide that needs to run CI for like three and four hours to build. And it was very flacky because, well, you know, Python packaging system is running some like Python codes in real time and it's very fragile. So like CI was always breaking. So we really hope to make things done in package themselves. We don't want it to maintain all the packages ourselves. So we want, for instance, NumPy to build Pyodide wheels themselves and SciPy too. And so that was that PAP 783. And we had one more years of discussions to make it accepted thanks to a lot of work by Hood. And we also standardized a lot of our build system so that people can actually easily build their packages into a PyPI scripting platform without setting up a lot of things in their machine. They can actually just use a cibuildwheel to build PyMD builds in their CI. They just need to add one PyMD platform and that's it. They can build PyMDs and builds in their CI.
Yeah, this is one concern I have been going back and forth with Jay about is that there's still a lot of compiler regressions. So like when we were doing the Python 3.15 update, you know, and I was trying to get up to the most recent version of mscripten. And it was like, you know, I had to go up like 10 versions, 10 releases, and almost every single one of them had like some weird regression in it. And that broke builds of some package. And so what's really still the big difficulty for us is that we need to make sure that we find these problems. And then I can put fixes for them into our compiler tool chain, our tooling that says like, oh, there's this weird bug in the compiler. But if we edit the compiler arguments, or we rewrite something a little bit, then it will go away and the users won't see it. or we can go up to the compiler and say, like, you know, hey, like, let's patch this. Or, you know, I mean, I'm a maintainer of Unscripted now, so I can, you know, help drive it into a non-buggy direction. But my concern is very much that we could specify an ABI and a platform and then not find out that it's broken until downstream package maintainers start trying to use it. And so that's the one sort of big issue that we need to figure out is like, how do we make sure that we have confidence in our platform? Which I think basically still means we have to carry builds for a bunch of packages just to make sure that we cover like all the different weird things that different packages rely on. Compilers have an amazing amount of surface area in terms of like what is expected for them to behave like.
Yeah, I was just going to say that I think the main thing that this packaging PEP really brings is that it does actually enable packagers and maintainers of packages to actually build for WebAssembly and for Pyodide, right? And it's not really, like, it doesn't really scale for Pyodide to be doing all of that themselves. So this is, like, really powerful to help spread the load and help all these, you know, the long tail of packages work
on Pyodide. So much. I had pulled up PyPI here, just as I was going to just bring it up. We're coming up million packages it doesn't make sense for you all try to bring this in into it yourself so if it's possible for whoever wants to be on mscripten and pyodide and so on to just do that by setting up ci build wheel and setting a wheel they're basically like it's awesome right yeah yeah
packages have Python scripting wheels now. So there is a website called rwepap783yet, which is created by another Pyodag maintainer. And it's already a lot of packages have Python scripting wheels, and some of them we are still maintaining ourselves. But it's, yeah, like, majority of the Python applications will depend on those top 500 Python packages. So you're trying to support those packages.
I guess it's probably worth pointing out that you all talked about this before, but just because something's pure Python doesn't mean that it's going to work on Pyodide, right? There's just parts that are missing because the runtime themselves are like, yeah, your OS thing, no. This comes from a browser or whatever, right? Or your raw socket support, I don't know about you.
right. If the package is depending on some native things like threadings or multi-processing or self-processing, and the native sockets will not basically work and browser Piodies. If you're running in Cloudflare Workers or Node.js, they will work because Node.js have access to some of them and still threadings or sub-processes are not working, but some of them will work depending on the platform you're running in Piodies.
I do think there's interesting new syntax that can be added. We've got the no coalescing or non coalescing operators and that kind of stuff possibly coming to Python. There will be things, but I don't think multi-line lambdas are going to be one of them. All right, let's talk about, we talked about a little bit, but let's go ahead and just talk a bit about edge workers in general. Because we talked about you've got over 300 points of presence. They're all near the users. That totally makes sense. but maybe compare and contrast this with like running on a VIP, just a virtual server or in Kubernetes or even serverless. Because to me, it feels kind of serverless-like, but not.
I think they're more serverless than Lambda as well. The only thing is that Lambda claimed the term serverless before, right? And so I think it's worth a little bit of a compare and contrast. So I'm a user I've written, or I'm a user I'm consuming in API, open up, I don't know, React front-end app, and it says, I need to do my work. I want to do it close to you. So what happens next? How do these things work? Well,
Yeah. But as a developer, I don't make that choice, right? It's like a dynamic DNS choice that says, I know you asked for, I don't know, worker.collectifair.com slash whatever, but you actually get Southeast Asia in this location. And that's just kind of the magic of CDNs, right? Yeah. Okay. So let's talk this back up.
You don't build any WebAssembly things yourself. Like all the WebAssembly things are already pre-built. So the deploying Python workers is basically you write some Python codes. And you need to, if you're using some Python packages as dependencies, you need to install them through uv. So uv has a support for like installing Python packages that are built for WebAssembly into your local files. Of course, you cannot run it inside your local machine. like you'll only have like a native Python interpreter, or you can use Pyodite to run it. So basically, we like compress all those, your Python applications and packages into a Cloudflare web server, and it's distributed around the world. And the Cloudflare runtime will like download the application and run it inside Pyodite and Cloudflare runtime.
Yeah, so I mean, part of the worker's runtime is open source as worker D, if you want to look it up, it's on GitHub. And you basically run that locally. I mean, we have like a CLI that wraps it for you. So it's nice and easy called Wrangler. But we're moving to CF now as well. But basically, you just you just run it locally, you you edit the files of your worker, they get reloaded automatically. as you edit them, you just send requests locally and see what happens.
Yeah, I mean, that's pretty unique, right? Because if I was doing Lambda or I was doing EKS or something like that, it's not the same. I can install a Kubernetes cluster and run pods and stuff, but that's not the same as running in Amazon's Kubernetes service. And this sounds like it kind of is, which is pretty interesting. So let's talk about the platform a bit because this, I was a little bit unsure of, or just don't have a ton of experience. So it's one thing to run Python, on a point of presence and point sort of thing. But databases, is the database got a little bit my data? You know, I connect halfway around the world to Postgres and is that maybe not as good? So there's a bunch of interesting things as well. And so you all like numbering for your D1, R2 and so on. But I have this crazy fascination with SQLite right now. So this is especially interesting to me. So let's talk about some of these building blocks that not just Python workers use, but all of them. But at the same time, they're here for the Python workers now. And so that's pretty interesting. Who wants to introduce D1 to the world? Dominic.
Sure. I mean, I can give it a shot. So I personally love SQLite. And I've actually been writing web apps using it, just even before running on workers, because it's so versatile. It's so easy to set up. You just load the library and save the database on your local file system. And I think this is basically an extension of that. I mean, you just get a local to your worker SQLite database. You can write to it. It gets replicated across the world for you. So, you know, and it scales with your workers as well. You don't have to worry about that at all.
It's pretty wild because you've got like 300 database nodes, which is pretty incredible. And for people who don't know, they might think of SQLite as a toy. It is extremely fast as a database. So it's pretty interesting, I think. Now, the one thing that seems tricky is somebody in Southeast Asia and somebody in London writes to the database at the same time. Now what?
So something I've been looking at lately is Lightstream actually been setting this up. So Lightstream is really interesting because it works on SQLite and you set up the write ahead log, the wall in SQLite, it will basically synchronize and stream that write ahead log off to somewhere else for backup and replication and so on. So I'm wondering, my first thought is probably they're doing something with the write ahead log and then merging, you know, synchronizing that and then merging, combining it or something like that. I'm not totally sure, but this is my theory.
Yeah, exactly. It's awesome. All right, we got a couple more. Hyperdrive. So D1 is kind of SQLite Hyperdrive. And this, I think, is new as part of your launch, right? This is kind of like distributed Postgres, but that wasn't available before. I don't know exactly, right? Something like this?
Yeah, I think a hyperdrive is relatively new. And, and it's kind, I think it's kind of the proxy, I think I can explain as a kind of proxy that there's like a truth of source, related database like PostgreSQL, MySQL, Summary, and the Summary. And we have a lot of like proxies in the edge that can basically like make your database connections a lot more faster, like each there's like 350 hyperdrives that like maintains the connection pool with the original database, like making the read and write a lot faster.
Yeah, I would just add to that. It's worth noting that we talked a little bit about sockets before and socket support in Piodide. And that is actually what I think, Jay, you've been working on, right? making sockets work on workers for Python. And that is what makes Hyperdrive work. Hyperdrive is built on these TCP sockets. So without that, it wouldn't work.
Yeah, exactly. Exactly. So supporting Hyperdrive in Cloudflare Python workers and in Pyrodite was one of the main goals for us before launching it at GA. And Pyrodite did not have socket support because browsers never had that. But to use the relational databases, you definitely need TCP connections and sockets. So we wired up the Node.js socket APIs inside Pyodide. So workers' runtime support Node.js socket APIs. So they are actually wired up inside Pyodide. So that by calling socket syscalls inside Pyodide actually calls a Node.js socket calls. So that's how AverageRM works.
This is really crazy because the socket APIs in Node, of course, are all asynchronous, but most of the socket system calls that you have in Linux are synchronous calls. And so in order to handle the system call, the system call has to stack switch and let the JavaScript event loop turn so that it can get the IO completed and then it unstacks switches and returns to the...
So that's what we've done. So we have our event loop in Python, which we use for PyTag, which we call WebLoop. And it basically just subscribes to the browser event. And this is essentially necessary for two reasons. One is that all actual IO happen on the browser loop. Like there is really no way, like if you can make your own like separate loop, but like it can't do IO. And while it's spinning the real IO, it's blocking the real event loop, which can't do the real IO. Yeah. And I said there were going to be two reasons, but I feel like that was both reasons.
No egress fees. Okay, very interesting. All right. And you can use this. Some of these things, I guess probably almost all of them, you can use regardless of whether you're using Cloudflare workers or not. I could use R2 just because I want object storage. Interesting. I'm about to think more about that. It's giving me ideas. Okay. And you've got queues, Cloudflare queues, and AI inference. So these are all the... I kind of wanted to go through these because these are the building blocks or the platform pieces that are available to these new Python workers, right?
Yeah, you have a lot of building blocks. inside Cloudflow. Developer platform, we keep adding it every month. So I'm not, to be honest, not following all those releases, but queues and AI and their workflows, things like that. But you can easily connect your workers with those new features and to build a full stack application. If you need a queue, you can use queue. If you want to run AI inference into Edge, you can use Worker's AI, which is awesome.
Yeah, that's super, super neat. All right, let's go to the announcement. And here it is. September 21. Took us a little bit to actually press record, but it's been a few weeks, not too long though. This announcement by the three of you, Python web workers are now generally available. Hooray! That's awesome. How long had they been in beta style availability before that?
I think the packaging story was actually a big thing. I mean, when we started, we, so there was two parts to this. One was trying to make sure that the cold starts are as fast as possible and especially when we're using packages. And when we started, we figured we'll just allow people to use certain built-in packages that Pyodite provides. But we just realized that that isn't flexible enough and we just need to allow people to use whatever packages they want, ideally. So we basically switched away from that approach to building a UV-based tool called PyWrangler, which lets you just install any package that you want and get it into your worker very easily. And then there's the memory snapshots aspect of it, which I think we might get to later. Oh, yeah.
Yeah, right. So this was about the fact that there is an important difficulty that comes up with memory snapshots, which is that it introduces a difference between sort of startup time or like, you know, sort of like top level execution and like your actual like runtime logic. And the Python language does not have a distinction like this, right? Like the Python runtime, when you start it up, it bootstraps itself. And then it just starts evaling user code. And whether it's top level user code or calling a function, it doesn't matter. Like it just evals until it stops evaling. And when it runs out of stuff to do, it shuts down. Yeah,
It's just right. Like the import statement is compiled into something. And then like those op codes get like interpreted. And that causes import lib to go look up files on disk, find the thing that it wants to import. And then it like starts executing that file. And so for memory snapshots, you know, we say like, okay, look, all of that top level stuff, it's going to happen the same every time. So it's very wasteful to keep doing it every time someone's deployed an application to the edge. Like every single time it cold starts, it's going to execute exactly the same top level code and end up with exactly the same outcome. And so we would really like to just save that work. And this is actually very similar to the work that like optimizing Python compilers like PyPy and Grawl Python do, because they also want to evaluate all that stuff at compile time. This is like partial evaluation. So it's a bit of a different technology than a memory snapshot. But it has the same fundamental issue, which is that it needs to impose a model of like what is startup time, what can be precompiled onto a language that doesn't have one. Now, of course, you know, I think this is this is this talk is like a fun thing to think about. But, you know, I mean, at least Thomas Vouders said that it was like a nonstarter. And I think he is right that like, you know, it's sort of too late for the language to get something like this. But I have friends that are working on on, you know, new programming languages. My friend Antonio Cooney is trying to make this language called SPI, which stands for static Python, which is supposed to be like a better R Python, which was used for PyPy. And basically, the main idea in some sense of the language is to pre-bake a notion of compile, to create a notion of compile time versus execution time, and then allow lots of compile time evaluation of spy code so that it can perform that.
Yeah, the basic idea is something like you're going to load the interpreter, it's going to load all the standard library, it's going to maybe read some of the files, parse them, do the import processing. And that never changes, right? Well, maybe the import, because it depends if you run code or not. But other than that, you theoretically could say, let's just see what happens. And like a VM, just go, your memory's back. Continue. Yeah?
Right. And of course, there's a lot of subtlety in there because we have, for instance, if you import a dynamic library, it does all this relocations. It allocates space for all the global variables that the dynamic library is going to define. And then it relocates them all into that space. And it, you know, relocates all the functions from the dynamic library. And somehow you have to do some crazy magic with the dynamic loader. Basically, like I have a loader patch that when it loads a dynamic library, writes down the relocation base that it's putting the dynamic library at. And then when I'm going to restore the memory snapshot, I have to make sure that it gets relocated to the same place. So I patch the malloc so that it will, instead of actually doing a memory allocation, it just looks, oh, in this context, I'm allocating memory for this dynamic library. In the snapshot, we allocated it at this address, so we're going to play back that address. And then all this crazy magic just sort of...
Yeah, I think the major usage of workers runtime is writing an API server. So we used Face the API a lot in our examples. So a lot of Python users might use Python in different ways, but I think the major use case of workers runtime is to build an API server and connecting different findings in it. So yeah, that's the examples we show in our flow post.
Yeah, you want your API to be super snappy and responsive. So if you could put your data in D1 and then have your code also run right literally in the same process right there, like that's going to be really great. Yeah. So you can, if you know how to write a FastAPI app, it's the same thing. Just instead of uvacoron.run, it's just hand off the app and off it goes, which is really, really neat. Now, the examples that I saw on this page, I don't remember, I'm pretty sure they all have like one request, just like forward slash. can I have a more complicated app with, say, five or six different endpoints? Yeah, that's just per short, yes, right?
Yeah, there is no limits to how many endpoints you can have. I suppose it might be worth talking about the actual limits that the workers have. And that is, yeah, so it's memory, which is 128 megabytes, and CPU time as well. You know, I think actually a great thing about workers is that you can do quite a lot free, like completely for free, you don't have to pay anything to Cloudflare, you can run a very simple FastAPI application, even I think, for free.
Yeah, yeah, you get 100,000 requests, I think, per day. And yeah, if you type in workers limits into Google, it should show up. But if you know, if you're just doing kind of more CPU intensive stuff, then that is another limit that we have. Yeah, it's basically there 10 milliseconds for free of CPU time and five minutes for paid. But yeah, you can do quite a lot. It's very, very flexible.
Right. Speaking of what we were doing for those two years, another thing that took a lot of careful work was making sure that when you run out of that 10 milliseconds of CPU time that a signal is delivered to the Python runtime, and it gracefully interrupts its execution and returns to Cloudflare so that it doesn't like leave it in a broken state. Because if you do this the naive way, JavaScript throws a non catchable exception into the runtime and forcibly unwinds all of the call stack without executing finally blocks or, you know, catch blocks. And so like any locks that you've taken that you were expecting to release in a finally block, that just doesn't get executed. And then the runtime is like completely broken. And so
sorts of things, you know, like loss of code relies on finally blocks executing and it was not executing the finally blocks. And so we had to like do some very elaborate thing to make the CPU time limiter deliver the signal correctly to the Python M-scripted operating system inside of the runtime. Of course, we have the same problem with Rust workers. So we basically have the same solution for both Python workers and for Rust workers for that.
Right. I think the good part of for like hobby developers is we have really generous pricing. I don't want to be too marketing pitch, but I also used Qualifier when I was a student because they really offered a good free pricing. Like you can have like 10 gigabytes of R2 for free, I guess. So there's a storage, there's a runtime, there's everything you can use freely.
Yeah, well, and you know, the storage is one thing, but the egress, that's where you pay money often for S3 is you pay tons of bandwidth charges. Yeah, very neat. All right, let's see. Let's go back to this. So first class language, really good. FastAPI, Django, Flask. Does it basically run anything that does WSGI or ASCII as each return that?
It's amazing for us. Yeah, because they don't try to do IO directly. And so, you know, I mean, that's exactly the problem we have with something like URL of three. Of course, URL of three specifically supports Pyodide. So it's okay. But like, it's normal way of doing IO just doesn't work. So it needs a separate path for Pyodide. And all of these things would need that if it weren't for these beautiful adapters.
that... Yeah, they're super polished and optimized. It's really nice. Yeah. Let's talk. We only got a couple minutes left before we're out of time. It's late in some places, early in others. Let's maybe round out this conversation by the types of apps that you see people building in Cloudflare workers in general, and specifically also in Python. Everyone knows what an API that talks to a database looks like, but can I do data science work here? Can I do long-running work? What types of things are people building here?
A lot of the workers that I've looked at are big piles of boring business logic. It's just a mess, and it does apparently what they are needing. I would not recommend data science. It's not really a platform that has a lot of computing power. If you want to do things like graphics cards, SIMD, it doesn't have any of that. But it's a very powerful platform for stuff.
That's totally fair. And I think that's one of the things I was trying to kind of explore is, right? Like how far can you push it before it just starts timing out or whatever, it runs out of memory. But what if I'm, let's say I'm a researcher or a student and I've got a pandas or just a parquet file with 20,000 records. And I do basic, like, here's a, let me do a little bit of query on that then I'll draw a graph or whatever.
Right. You can load pandas. You can load, you know, whatever the parquet package that is popular now is. And, you know, I mean, just the packages, you know, if you start, you know, like, for instance, SciPy is just too big, too horrible. And it doesn't really work to import SciPy. I mean, SciFi, when you bring it in, it has like over 100 different shared libraries. You know, yeah, SciFi has been like a real journey for us in Pyodide. Like it's really like the most horrible package. But if it's not SciFi, you know, Pandas works and you can just, you know, load your Parquet file and share your graph as long as it's not too big.
Well, in my opinion, like pandas or docdb or other data processing libraries are not very optimized for Python workers. It's because like there are different, like the concept is a little bit different because workers is very memory limited environment. So we basically recommend everything to run in streams, not like load a bunch of memory, like lots of data frames inside memory.
Yeah, like, yeah, more polars, not pandas. if you need to load all the data frames in your memory, that it won't fit. You need to do streams. But actually, like Python packages are, especially legacy Python packages, are not very optimized for that. So we'll need to do a lot of back and forth to make it work properly.
So we're talking about Pyodide and WebAssembly. Another way, if I was this theoretical person I was describing, wanted to put this stuff on workers, maybe what I do is I just put my parquet file and my WebAssembly stuff on Cloudflare and just put it on the front end, right? You could run it like a JupyterLite style of execution. And then everyone gets their own little browser runtime instead of Edgeware runtime.
That's your iPhone battery or your iPad battery or whatever, rather than your compute bill. Because setting up that kind of stuff is really tricky for researchers who are not developers and they're not DevOps people. They're just like, I just want to share this thing, right? So maybe that's the way to go. All right.
I've talked to so many people who are doing that, like sharing their data science notebook, like instead of having to have a Jupyter notebook and then they make it and then their collaborators don't have the right things on their computer or it doesn't quite work. You know, this is exactly the same sorts of problems I saw in math all the time. And, you know, these internet-based tools are just like so great for them You know, and it really is like what happens with like a math or science project is like if there's a lowest common denominator effect where like the technology you can use your research is only the things that the least technically competent person in your group can use. Because if you can't get get it onto their computer and get it working for them, then they won't be able to productively contribute to a project that needs it.
some of these people literally like it's like they have they like dictate to their secretary and like they've never touched a computer. And some of these people like contribute to the Linux kernel like regularly. Yeah, exactly. They both look like a 67, like nerdy mathematician, but like, just like the farest ends of the scale of like confidence with computers. Incredible.
All right, you guys, let's call it there. There's a lot of stuff that we could talk about. I guess one really, really quick thing I just want to give a shout out to is this memory snapshot stuff that we talked about. Hood, you all are using that for basically increasing the startup time, the cold start time.
Well, I was just going to say, actually, I think the best thing for workers is IO-bound applications. if you have something that's like pushing a lot of bytes, isn't doing a lot of CPU with them, then give it a try. I think you'll be surprised how far you can go how many users you can handle even on the free tier. And yeah, just check it out type in Python workers into Google and give it a try.
And I just want to advertise to anyone out there that WebAssembly is like a really cool ecosystem. There's a lot of missing pieces in terms of like fundamental computing. So if you like operating systems, if you want to think about like how LibC works or, you know, low level details, and, you know, you feel like Linux works too well for you to get into, then like I think WebAssembly is a really great space because a lot more of that stuff is just I think no one has ever even tried to make it work. There's not that much effort. The total number of people working on it is quite small. So there's lots of open niches. And if you find something that you wish worked that doesn't work, you can just sort of wedge yourself in there and fix things. So I think it's a great space.
Excellent. All right. Thank you all for being on the show. I learned a lot. I didn't point out at the beginning, I should have, that I'm a complete newbie at all this edge worker stuff. So it was really interesting to get a look into it. And it's awesome to see it come into the Python space. See you all later.
This has been another episode of Talk Python To Me. Thank you to our sponsors. Be sure to check out what they're offering. It really helps support the show. Tomorrow, Wednesday, November 4th, we're having our first tech meetup, our live tech event as part of Talk Python membership. If you're not yet a member, well, join up and support the show and get an awesome community and experience to boot. Just visit talkpython.fm, Click membership in the nav bar and you'll be off to the races. If you are a member, we'll be talking about Talk Python in production and some really wild new ideas that I'm exploring. I think you will find both fascinating and actually really practical, but no one's doing it. So join me on Wednesday, November 4th. And if you're listening to this after and thinking, Michael, why did you just put this in here? Nobody can listen to it because most people listen to after. These tech meetups come with recordings. Just visit talkpython.fm, click on membership, and then browse past member meetups and you'll be able to get it. So hopefully either you can join live or you can join afterwards. Become a member, support the show, join the event. If you or your team needs to learn Python, we have over 270 hours of beginner and advanced courses on topics ranging from complete beginners to async code, Flask, Django, HTMX, and even LLMs. Best of all, there's no subscription in sight. Browse the catalog at talkpython.fm. And if you're not already subscribed to the show on your favorite podcast player, what are you waiting for? just search for Python in your podcast player. We should be right at the top. If you enjoyed that geeky rap song, you can download the full track. The link is actually in your podcast player share notes. This is your host, Michael Kennedy. Thank you so much for listening. I really appreciate it. I'll see you next time. I'm out.
