跳转到内容
搜索文档

DAG 工作流

最后更新 查看 MarkdownAgent 设置

Python Workflows SDK 以声明式方式支持 DAG workflow,使用 step.do 和参数名称定义依赖关系(步骤运行前必须完成的其他步骤)。

from workers import Response, WorkflowEntrypoint, WorkerEntrypoint

class PythonWorkflowStarter(WorkflowEntrypoint):
    async def run(self, event, step):
        async def await_step(fn):
            try:
                return await fn()
            except TypeError as e:
                print(f"Successfully caught {type(e).__name__}: {e}")

        await step.sleep('demo sleep', '10 seconds')

        @step.do()
        async def dep_1():
            # does stuff
            print('executing dep1')
            return 'dep1'

        @step.do()
        async def dep_2():
            # does stuff
            print('executing dep2')
            return 'dep2'

        @step.do(concurrent=True)
        async def final_step(dep_1, dep_2):
            # does stuff
            print(f'{dep_1} {dep_2}')

        await await_step(final_step)

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

在此示例中,dep_1dep_2final_step 执行之前并发运行,而 final_step 依赖两者。

设置 concurrent=True 允许并发解析依赖关系。如果依赖已完成,将跳过并重用其返回值。

此模式适用于菱形 workflow,其中一个步骤依赖两个或更多可以并发运行的其他步骤。

这篇文档对您有帮助吗?