Cloudflare Python Workers - Talk Python to Me Ep. 568
Watch on YouTube →
Overview
Dominic, Jay, and Hood explain how Cloudflare Python Workers run Pyodide and WebAssembly inside Cloudflare's globally distributed Workers runtime, bringing Python execution close to users across more than 350 points of presence. They cover Pyodide's packaging and compatibility work, deployment and local development, services including D1 and Hyperdrive, and performance constraints such as 128 MB memory and CPU limits, with memory snapshots helping reduce startup work.
Key takeaways
- Cloudflare Python Workers run Pyodide through WebAssembly on V8 at more than 350 edge locations, so Python code can execute near users without developers choosing a deployment region.
- PEP 783 shifts Emscripten wheel building toward package maintainers, addressing Pyodide's earlier burden of building roughly 300 packages in CI over three to four hours.
- Workers can connect Python database code to Cloudflare services: D1 provides SQLite-based storage, Hyperdrive proxies connections to PostgreSQL or MySQL, and R2 offers S3-compatible storage without egress fees.
- Python Workers suit I/O-bound services better than large in-memory data science jobs: the discussed limits include 128 MB of memory and 10 milliseconds of free CPU time per request.
- Memory snapshots reduce repeated startup work, but restoring Python and dynamically loaded libraries requires preserving allocation and relocation details that ordinary Python execution does not model explicitly.
- Pyodide package support depends on the host runtime: asyncio works through integration with the JavaScript event loop, while features such as threads, subprocesses, and native sockets vary by environment.
Chapters
- Dominic moved from Visual Basic .NET to Python, later joined Cloudflare's Workers runtime team, and experimented with running Pyodide there.
- Jay began using Python for university data science with NumPy and SciPy, then contributed to Pyodide before joining Cloudflare.
- Hood, a mathematician, pursued WebAssembly and Pyodide to make mathematical software easier to distribute and use in browsers.
- Pyodide compiles Python for WebAssembly through Emscripten and can run in browsers or JavaScript environments such as Node.js and Bun.
- Cloudflare Workers place that runtime on edge infrastructure, rather than inside each user's browser.
- Cloudflare's more than 350 points of presence let Workers execute near users and connect to platform services.
- Hood says Pyodide has become more polished, with fewer recurring bugs and a clearer maintenance path alongside upstream Python.
- CPython treats Emscripten as a tier 3 platform: it runs in CI, but support is best effort and does not block releases.
- Hood submitted PEP 818 to add Pyodide's JavaScript foreign-function interface to Python, enabling Python code to call JavaScript APIs.
- Packages containing C, C++, or Rust code must be cross-compiled for WebAssembly; pure Python packages avoid that build step but may still rely on unsupported system features.
- Before PEP 783, Pyodide's CI built about 300 packages, taking three to four hours and creating fragile, slow test runs.
- PEP 783 standardizes Emscripten packaging so package maintainers can build and publish compatible wheels themselves, including through cibuildwheel.
- Compiler regressions remain a concern, so Pyodide still needs broad package coverage to test the platform's real-world compatibility.
- Pure Python code is not guaranteed to work: packages depending on subprocesses, multiprocessing, threads, or native sockets may be incompatible with a given runtime.
- Pyodide's WebLoop connects Python's asyncio behavior to the JavaScript event loop, which is necessary for browser-based I/O.
- Node.js and Cloudflare Workers can expose capabilities unavailable in browsers, so package compatibility depends partly on the JavaScript host.
- A request reaches a nearby Cloudflare point of presence, where the Workers runtime runs on V8 and executes Pyodide through WebAssembly.
- Unlike region-based serverless deployments, Workers route code to users without developers selecting a fixed data-center region.
- Hood describes the model as a CDN that distributes programs and runs them against data, not just a network for static assets.
- Developers write Python applications and use uv to install compatible WebAssembly package dependencies; they do not compile WebAssembly themselves.
- Cloudflare distributes the application and packages, which the Workers runtime loads into Pyodide.
- The open-source workerd runtime supports local execution; Wrangler provides a development workflow with automatic reloads and local requests.
- D1 offers SQLite-based databases integrated with Workers, including replication across Cloudflare's global network.
- Hyperdrive proxies connections to existing PostgreSQL or MySQL databases, maintaining edge connection pools and optionally caching reads.
- R2 provides S3-compatible object storage without egress fees, and can be used independently of Workers.
- Cloudflare also offers platform components such as Queues and Workers AI for background work and inference.
- Pyodide does not provide browser-style native TCP sockets by default, but Cloudflare Workers can expose Node.js socket APIs.
- Jay describes wiring those APIs into Pyodide so Python socket calls can support database connections and Hyperdrive.
- WebLoop integrates Python's asyncio with the JavaScript event loop, allowing asynchronous I/O to complete without blocking the host runtime.
- Python Workers reached general availability after a beta period that began around spring 2024; development had started in October 2023.
- The team replaced a limited built-in-package approach with PyWrangler, a uv-based tool for including user-selected dependencies.
- Memory snapshots preserve repeatable startup work so workers do not need to re-execute the same initialization on every cold start.
- Snapshot restoration required careful handling of dynamic-library loading and memory addresses, and raises a distinction between startup code and runtime logic.
- Python Workers can run FastAPI applications and support general ASGI and WSGI adapters, allowing multiple routes and standard Python web frameworks.
- The discussed limits include 128 MB of memory, 100,000 requests per day on the free tier, 10 milliseconds of free CPU time, and up to five minutes on paid plans.
- Cloudflare engineered CPU-limit interruptions to reach Python safely, allowing cleanup such as finally blocks instead of leaving the runtime in a broken state.
- Dominic notes that even simple FastAPI applications may fit within the free request allowance.
- Dominic recommends Workers for I/O-bound applications that move many bytes but perform relatively little CPU-intensive processing.
- The 128 MB memory limit makes large in-memory data science workloads a poor fit; streaming data is preferred over loading full datasets.
- Pandas or Parquet workflows may work for modest inputs, while Polars-style streaming is a better match than memory-heavy processing.
- For shareable research notebooks, Pyodide in the browser can move computation to users' devices instead of consuming Workers resources.
- Dominic and Jay encourage developers to try Python Workers for hobby applications and report issues through Cloudflare's Discord.
- Hood highlights WebAssembly as an open area for contributors interested in operating systems, libc, compilers, and low-level runtime behavior.
- The team identifies memory snapshots as a way to avoid repeating initialization and improve worker startup time.
Transcript excerpt
0:02 Dominic J Hood, welcome to all of you to talk Python to me. Awesome to have you here. It's nice to be 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, it 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.
0:55 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 NIM 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.
1:54 And we started Python workers and I've been working on that ever since as well and still do.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Talk Python.