跳转到内容
搜索文档

Python Workflows SDK 概览

最后更新 查看 MarkdownAgent 设置

Workflow 入口点可以使用 Python 声明。为此,你可以导出在 Cloudflare Workers 平台上运行的 WorkflowEntrypoint。 有关 Workers 运行时上的 Python 的更多信息,请参阅 Python Workers

快速入门

Python workflow 的主入口点是 WorkflowEntrypoint 类。你的 Workflow 逻辑应存在于 run 处理程序内。

from workers import WorkflowEntrypoint

class MyWorkflow(WorkflowEntrypoint):
    async def run(self, event, step):
        # steps here

例如,Workflow 可以定义如下:

from workers import Response, WorkflowEntrypoint, WorkerEntrypoint

class PythonWorkflowStarter(WorkflowEntrypoint):
    async def run(self, event, step):

        @step.do('step1')
        async def step_1():
            # does stuff
            print('executing step1')

        @step.do('step2')
        async def step_2():
            # does stuff
            print('executing step2')

        await step_1()
        await step_2()

class Default(WorkerEntrypoint):
    async def fetch(self, request):
        await self.env.MY_WORKFLOW.create()
        return Response("Hello world!")

你必须在 Wrangler 配置文件中同时添加 python_workflowspython_workers 兼容性标志。

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "hello-python",
	"main": "src/entry.py",
	"compatibility_flags": [
		"python_workers",
		"python_workflows"
	],
	// Set this to today's date
	"compatibility_date": "2026-08-17",
	"workflows": [
		{
			"name": "workflows-demo",
			"binding": "MY_WORKFLOW",
			"class_name": "PythonWorkflowStarter"
		}
	]
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "hello-python"
main = "src/entry.py"
compatibility_flags = [ "python_workers", "python_workflows" ]
# Set this to today's date
compatibility_date = "2026-08-17"

[[workflows]]
name = "workflows-demo"
binding = "MY_WORKFLOW"
class_name = "PythonWorkflowStarter"

要在本地运行 Python Workflow,请使用 Wrangler(Cloudflare Workers 的 CLI):

npx wrangler@latest dev

要将 Python Workflow 部署到 Cloudflare,运行 wrangler deploy

npx wrangler@latest deploy

加入 Cloudflare Developers Discord 的 #python-workers 频道,告诉我们你希望看到什么。

这篇文档对您有帮助吗?