Cloudflare Workers 支持 Python 与 JS 跨语言
Workers - Python and JavaScript Workers can now call each other via RPC
Cloudflare Workers 用户现在可以在 Python 和 JavaScript 之间直接 RPC 调用,无需额外依赖,显著简化跨语言服务集成。建议开发者评估现有 Worker 架构,利用此能力优化服务间通信。
You can now call methods between Python and JavaScript Workers using Workers RPC. This works through Service bindings without extra dependencies, schema definitions, or serialization code.
Cross-language RPC calls behave like ordinary function calls. Exceptions propagate to the call site. You can pass structured cloneable types ↗ as parameters or return values, and Pyodide Foreign Function Interface (FFI) automatically converts types between languages.
Call a TypeScript Worker from Python
Define a method in a TypeScript Worker:
index.jsjs
import { WorkerEntrypoint } from "cloudflare:workers";
export class RpcService extends WorkerEntrypoint {
async add(a, b) {
return a + b;
}
}index.tsts
import { WorkerEntrypoint } from "cloudflare:workers";
export class RpcService extends WorkerEntrypoint {
async add(a: number, b: number): Promise<number> {
return a + b;
}
}Call it from a Python Worker through a Service binding:
from workers import Response, WorkerEntrypoint
class Default(WorkerEntrypoint):
async def fetch(self, request):
rpc = self.env.RPC
result = await rpc.add(42, 144)
return Response.json({"result": result})Configure the Service binding in the Python Worker's Wrangler configuration:
{
"services": [
{
"binding": "RPC",
"service": "ts-rpc-server",
"entrypoint": "RpcService"
}
]
}[[services]]
binding = "RPC"
service = "ts-rpc-server"
entrypoint = "RpcService"Call a Python Worker from JavaScript
Define a method in a Python Worker:
from workers import WorkerEntrypoint
class Default(WorkerEntrypoint):
async def highlight_code(self, code: str, language: str) -> dict:
from pygments.formatters import HtmlFormatter
from pygments import highlight
from pygments.lexers import get_lexer_by_name
lexer = get_lexer_by_name(language, stripall=True)
formatter = HtmlFormatter(linenos=True, cssclass="highlight", style="monokai")
highlighted_html = highlight(code, lexer, formatter)
css = formatter.get_style_defs(".highlight")
return {
"html": highlighted_html,
"css": css
}Call it from a JavaScript Worker through a Service binding:
index.jsjs
export default {
async fetch(request, env) {
const rpc = env.PYTHON_RPC;
const result = await rpc.highlight_code("print(42)", "python");
return Response.json(result);
},
};index.tsts
export default {
async fetch(request, env) {
const rpc = env.PYTHON_RPC;
const result = await rpc.highlight_code("print(42)", "python");
return Response.json(result);
},
};Configure the Service binding in the JavaScript Worker's Wrangler configuration:
{
"services": [
{
"binding": "PYTHON_RPC",
"service": "py-rpc-server"
}
]
}[[services]]
binding = "PYTHON_RPC"
service = "py-rpc-server"For more details on the announcement, read the blog post ↗.
For more information, refer to the Workers RPC documentation and the Python Workers overview.
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力