Cloudflare Python Workers 正式 GA
Python Workers are now generally available
Python 开发者关注度高,这次 GA 解决了长期存在的生态兼容痛点,特别是原生绑定支持和 WSGI/ASGI 直连能力,值得相关技术栈从业者重点关注。
We introduced Python Workers two years ago, providing a way to run Python applications in the Cloudflare Workers runtime. Our goal was to make it as simple to write Workers in Python as it is in TypeScript, and to make the ecosystem of Python packages and frameworks “just work”.
我们于两年前推出了 Python Workers,提供了一种在 Cloudflare Workers 运行时中运行 Python 应用程序的方法。我们的目标是让用 Python 编写 Workers 与使用 TypeScript 一样简单,并使 Python 包和框架的生态系统能够“开箱即用”(just work)。
Today, Python Workers are now generally available (GA).
今天,Python Workers 现已正式全面可用(GA)。
What does GA mean? It means Python is now a first-class, fully supported language on the Cloudflare Developer Platform. You can bring the Python code, libraries, and design patterns you already know and connect them seamlessly to Workers AI, R2, D1, Hyperdrive, Durable Objects, Queues, Workflows, and the rest of the Cloudflare platform. You can also run popular Python frameworks like FastAPI, Django, and Flask inside Python Workers. You can even create a Python Worker inside another Worker using Dynamic Workers.
GA 意味着什么?这意味着 Python 现在已成为 Cloudflare Developer Platform 上的一等公民、完全受支持的语言。您可以将已有的 Python 代码、库和设计模式引入其中,并无缝连接到 Workers AI、R2、D1、Hyperdrive、Durable Objects、Queues、Workflows 以及 Cloudflare 平台的其余部分。您还可以在 Python Workers 内部运行 FastAPI、Django 和 Flask 等流行的 Python 框架。您甚至可以使用 Dynamic Workers 在另一个 Worker 内部创建一个 Python Worker。
The journey behind Python Workers
Python Workers 背后的发展历程
Bringing Python to Cloudflare Workers was a natural choice. Because Workers has supported WebAssembly since 2018, it gave us the perfect environment to run a Wasm-compiled Python interpreter. By using Pyodide, we were able to quickly support a wide range of Python applications in Cloudflare Workers.
将 Python 引入 Cloudflare Workers 是一个自然的选择。由于 Workers 自 2018 年起就支持 WebAssembly,这为我们提供了一个完美的环境来运行经过 Wasm 编译的 Python 解释器。通过使用 Pyodide,我们能够迅速在 Cloudflare Workers 中支持广泛的 Python 应用程序。
Our goal was to create the first platform for infinitely scalable Python apps, while making it as easy and performant as developing Python apps anywhere else.
我们的目标是打造一个无限可扩展的 Python 应用平台,同时使其开发体验与其他任何地方开发 Python 应用一样轻松高效。
The features we are highlighting today are the result of this multi-year effort. Many developers are already building applications within Python Workers; today, we are making these capabilities production-ready for everyone.
今天我们重点介绍的功能是多年努力的结果。许多开发者已经在 Python Workers 中构建应用程序;今天,我们正在将这些能力变为面向所有人的生产就绪状态。
Python is now a first-class language in the Cloudflare Workers runtime
Python 现在已成为 Cloudflare Workers 运行时中的一等语言
Python Workers now natively support Cloudflare Developer Platform bindings. Previously, using these Cloudflare bindings in Python Workers required converting Python objects into TypeScript objects explicitly at the RPC boundary. For example, sending a Python dictionary into a Cloudflare Queue required the following glue code to work:
Python Workers 现在原生支持 Cloudflare Developer Platform 绑定。此前,在 Python Workers 中使用这些 Cloudflare 绑定需要在 RPC 边界处显式地将 Python 对象转换为 TypeScript 对象。例如,将 Python 字典发送到 Cloudflare Queue 需要以下胶水代码才能工作:
This required Python developers to keep the JavaScript environment and code in mind while writing Python Workers, and it was a common source of error for both humans and AI agents. To address this, we have encapsulated the entire type conversion process within the Workers runtime and the Python SDK. This allows you to utilize all Cloudflare bindings in a Pythonic way without writing a single line of JavaScript code, making the following just work:
这使得 Python 开发人员在编写 Python Workers 时必须时刻牢记 JavaScript 环境和代码,这也是人类和 AI 代理常见的错误来源。为解决这一问题,我们将整个类型转换过程封装在了 Workers 运行时和 Python SDK 内部。这使您能够以符合 Python 风格的方式利用所有 Cloudflare 绑定,而无需编写一行 JavaScript 代码,从而使以下内容能够正常工作:
Web frameworks: FastAPI, Django, and Flask
Web 框架:FastAPI、Django 和 Flask
You can now run your favorite Python framework, such as FastAPI, Django, or Flask, to build an API server in Python Workers. We implemented a built-in connector that you can use to easily connect your web application to Python Workers.
你现在可以在 Python Workers 中运行你喜爱的 Python 框架(如 FastAPI、Django 或 Flask),以构建 API 服务器。我们实现了一个内置连接器,你可以用它轻松地将 Web 应用程序连接到 Python Workers。
Let’s say you have a simple FastAPI web application:
假设你有一个简单的 FastAPI Web 应用程序:
In native environments, you would use a web server such as uvicorn to run this application.
在原生环境中,你会使用 uvicorn 等 Web 服务器来运行此应用程序。
In Python Workers, you can run the same application using the workers.asgi package we provide, just by adding this snippet to your code:
在 Python Workers 中,你可以使用我们提供的 workers.asgi 包来运行相同的应用程序,只需在代码中添加以下片段即可:
Similarly, you can use workers.wsgi package to run synchronous web applications such as Django.
类似地,你可以使用 workers.wsgi 包来运行 Django 等同步 Web 应用程序。
So, what happens under the hood?
那么,底层究竟发生了什么?
Python has a standard contract for how web applications should communicate with web servers, known as the Web Server Gateway Interface (WSGI), or its modern asynchronous counterpart, ASGI. This standard allows developers to build applications that are completely server-agnostic. In a traditional deployment, web servers like Uvicorn or Gunicorn are responsible for handling multiple concurrent client connections and threads to scale traffic, while web frameworks like FastAPI can focus purely on the application logic.
Python 有一个关于 Web 应用程序如何与 Web 服务器通信的标准契约,称为 Web Server Gateway Interface (WSGI),或其现代异步对应物 ASGI。该标准允许开发者构建完全与服务器无关的应用程序。在传统部署中,Uvicorn 或 Gunicorn 等 Web 服务器负责处理多个并发客户端连接和线程以扩展流量,而 FastAPI 等 Web 框架则可以专注于纯粹的应用程序逻辑。
In Cloudflare Workers, the Workers platform itself serves as the web server. Since our global network already seamlessly handles load balancing and infinite scaling, we don't need to reinvent the wheel by running a server inside Python Workers.
在 Cloudflare Workers 中,Workers 平台本身充当 Web 服务器。由于我们的全球网络已经无缝处理负载均衡和无限扩展,因此我们无需在 Python Workers 内部运行服务器来重复造轮子。
Instead, our workers.asgi and workers.wsgi connectors act as a thin, optimized bridge. They translate the incoming native JavaScript request into the standard WSGI/ASGI structures that Python applications expect, and seamlessly pipe the response back out with minimal overhead. By doing this, Python developers get the best of both worlds: you can write and organize code using your favorite web frameworks, while letting the Cloudflare Workers platform instantly scale your API across the globe, without ever configuring a server.
相反,我们的 workers.asgi 和 workers.wsgi 连接器充当了轻量级且经过优化的桥梁。它们将传入的原生 JavaScript 请求转换为 Python 应用程序期望的标准 WSGI/ASGI 结构,并以最小的开销将响应无缝地传回。通过这种方式,Python 开发者可以兼得两者之长:你可以使用自己喜爱的 Web 框架来编写和组织代码,同时让 Cloudflare Workers 平台在全球范围内即时扩展你的 API,而无需配置任何服务器。
These connectors can be used not only with FastAPI, Django, or Flask, but with any Python web framework that uses the WSGI or ASGI interface.
这些连接器不仅可与 FastAPI、Django 或 Flask 一起使用,还可与任何使用 WSGI 或 ASGI 接口的 Python Web 框架一起使用。
You can find more information about using each web framework in the Python Workers documentation.
你可以在 Python Workers 文档中找到有关使用每个 Web 框架的更多信息。
Using PostgreSQL and MySQL with Hyperdrive
在 Hyperdrive 中使用 PostgreSQL 和 MySQL
If you are building a Python application using relational databases such as PostgreSQL or MySQL, you can now integrate Hyperdrive into Python Workers.
如果你正在使用 PostgreSQL 或 MySQL 等关系数据库构建 Python 应用程序,现在可以将 Hyperdrive 集成到 Python Workers 中。
Previously, Python Workers didn’t support TCP sockets, making database drivers unavailable. To understand why this was a blocker, you need to look at how WebAssembly operates. Python database drivers like aiomysql or asyncpg rely on the standard library's socket module to establish connections. In a standard environment, this module makes POSIX system calls to the underlying operating system. Inside a WebAssembly sandbox, those POSIX networking syscalls are normally stubs that always fail. Any attempt to open a standard socket would immediately fail. To solve this problem, we implemented socket system calls using the Workers connect API.
此前,Python Workers 不支持 TCP 套接字,导致数据库驱动程序不可用。要了解这为何成为阻碍,你需要了解 WebAssembly 的运行方式。像 aiomysql 或 asyncpg 这样的 Python 数据库驱动程序依赖标准库中的 socket 模块来建立连接。在标准环境中,该模块会向底层操作系统发出 POSIX 系统调用。在 WebAssembly 沙箱内部,这些 POSIX 网络系统调用通常是始终失败的桩函数(stubs)。任何尝试打开标准套接字的操作都会立即失败。为了解决这个问题,我们使用 Workers connect API 实现了套接字系统调用。
When a database driver attempts to open a TCP connection, it goes through our custom socket syscall implementation. It translates standard Python socket operations like opening a connection and reading bytes into the corresponding JavaScript calls used by the Workers runtime. Because this translation happens at the system call level, your database drivers don't have to know about the underlying implementation at all.
当数据库驱动程序尝试打开 TCP 连接时,它会经过我们自定义的 socket 系统调用实现。它将标准的 Python 套接字操作(如打开连接和读取字节)转换为 Workers 运行时使用的相应 JavaScript 调用。由于这种转换发生在系统调用层面,你的数据库驱动程序完全无需了解底层实现。
This socket bridge is what makes our Hyperdrive integration possible. To use Hyperdrive in Python Workers, first connect your database with Hyperdrive and set up the binding in the Wrangler config:
这个套接字桥接功能使得我们的 Hyperdrive 集成成为可能。要在 Python Workers 中使用 Hyperdrive,首先通过 Hyperdrive 连接你的数据库,并在 Wrangler 配置中设置绑定:
Then, connect to Hyperdrive using the database drivers you are familiar with:
然后,使用你熟悉的数据库驱动程序连接到 Hyperdrive:
You can refer to the Hyperdrive Python Workers documentation to find out how you can use Hyperdrive in Python Workers, and which packages are currently supported.
你可以参考 Hyperdrive Python Workers 文档,了解如何在 Python Workers 中使用 Hyperdrive,以及目前支持哪些包。
Expanding the WebAssembly package ecosystem
扩展 WebAssembly 包生态系统
Because Python Workers run inside a WebAssembly sandbox, any packages with native C/C++/Rust extensions must be cross-compiled to WebAssembly to run in Python Workers. However, previously, there was no standard way to cross-compile any Python packages to WebAssembly. That meant our team had to manually compile and host custom WebAssembly packages. This greatly limited the number of packages you could actually use in Python Workers.
由于 Python Workers 运行在 WebAssembly 沙箱内,任何带有原生 C/C++/Rust 扩展的包都必须交叉编译为 WebAssembly 才能在 Python Workers 中运行。然而,此前没有将任何 Python 包交叉编译为 WebAssembly 的标准方法。这意味着我们的团队必须手动编译并托管自定义 WebAssembly 包。这极大地限制了你在 Python Workers 中实际可用的包的数量。
We wanted to fix this and allow users to use a wider variety of packages. However, we didn’t want to merely build packages usable only in Python Workers, which wouldn’t benefit the community. Since Python Workers are built on top of Pyodide, we wanted the ecosystem to evolve in a way that benefits Pyodide and the entire Python-on-WebAssembly community.
我们希望解决这一问题,让用户能够使用更多种类的包。然而,我们并不只想构建仅在 Python Workers 中可用的包,因为这无法惠及社区。由于 Python Workers 是建立在 Pyodide 之上的,我们希望生态系统的演进方式能够惠及 Pyodide 以及整个 Python-on-WebAssembly 社区。
To this end, we proposed PEP 783, which standardizes a platform for running Python in the browser runtimes called PyEmscripten. After over a year of discussion and refinement, this proposal was accepted, enabling package maintainers to build and publish packages for the PyEmscripten platform and make them available across all environments that implement PyEmscripten.
为此,我们提出了 PEP 783,该提案标准化了一个名为 PyEmscripten 的平台,用于在浏览器运行时中运行 Python。经过一年多的讨论和完善,该提案获得通过,使包维护者能够构建并发布适用于 PyEmscripten 平台的软件包,并在所有实现 PyEmscripten 的环境中提供这些软件包。
We also stabilized the existing Pyodide build toolchain and evolved it into a form that is accessible to all package maintainers, enabling developers to easily build packages for the PyEmscripten platform. Furthermore, we added PyEmscripten platform support to cibuildwheel, to make it easier for others to adopt support for the PyEmscripten platform.
我们还稳定了现有的 Pyodide 构建工具链,并将其演进为一种所有包维护者均可访问的形式,使开发者能够轻松地为 PyEmscripten 平台构建软件包。此外,我们在 cibuildwheel 中添加了对 PyEmscripten 平台的支持,以便他人更轻松地采用对 PyEmscripten 平台的支持。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力