prefect resume pause

c 复制代码
import json
import os
from datetime import datetime, timezone
from pathlib import Path
import time
from prefect import flow, task, apause_flow_run
from pydantic import BaseModel

class NextStepInput(BaseModel):
    next: str
    node: str = "default"


@task(persist_result=True) #
async def step1(message: str) -> dict[str, any]:
    time.sleep(2)  # Simulate some processing time
    print("step1 executed")
    return {
        "message": message,
        "created_at": datetime.now(timezone.utc).isoformat(),
        "steps":["step1"]
    }


@task(persist_result=True) #
async def step2(payload: dict[str, any]) -> str:
    time.sleep(2) 
    payload["steps"].append("step2")
    print("step2 executed")
    return payload

@task(persist_result=True) #
async def step3(payload: dict[str, any]) -> str:
    time.sleep(2) 
    payload["steps"].append("step3")
    print("step3 executed")
    # raise Exception("Simulated error in step3")  # 模拟异常,测试失败重试机制
    return payload


@task(persist_result=True) #
async def step4(payload: dict[str, any]) -> str:
    time.sleep(2) 
    payload["steps"].append("step4")
    print("step4 executed")
    output_dir = Path(os.getenv("DEMO_OUTPUT_DIR", "./runs"))
    output_dir.mkdir(parents=True, exist_ok=True)

    output_path = output_dir / f"prefect-demo-{payload['created_at'].replace(':', '-')}.json"
    output_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
    return str(output_path)

@flow(name="map_pipeline_flow", log_prints=True)
async def map_pipeline_flow(message: str = "Hello from local Prefect") -> str:
    payload = await step1(message)

    # suspend_flow_run()
    res = await apause_flow_run(wait_for_input=NextStepInput)
    print(f"Received input: {res}")

    if res.next == "step2":
        payload = await step2(payload)
    elif res.next == "step3":
        payload = await step3(payload)
    else:
        print("No next step specified, ending flow.")
        return "No next step specified"



    # suspend_flow_run()
    res = await apause_flow_run()
    print(res)

    output_path = await step4(payload)
    print(f"Saved payload to {output_path}")
    return output_path


if __name__ == "__main__":
    map_pipeline_flow()
相关推荐
星辰徐哥7 小时前
本地视频预览别只自己看:把Remotion动效项目发给客户远程验收
docker·ai·node.js·html·音视频·react·remotion
fb_1234511 小时前
Docker入门-第3章-容器镜像深度解析
运维·docker·容器
IT摆渡者12 小时前
基于 Docker 部署 Nextcloud 私有云盘(外挂 Windows 共享)项目实践
运维·docker·容器
新时代牛马14 小时前
单机 docker run 管不住了?从编排需求、期望状态到调度/自愈闭环讲透
运维·docker·容器
binqian14 小时前
Docker Desktop(WSL2 后端)三层网络互通技术文档
网络·docker·容器
LuiChun15 小时前
与精神病患者的日常探讨20260913
docker
闲云野鹤在人间15 小时前
Docker入门|第1章 容器生态系统完整解析
运维·服务器·docker·容器·centos
KIDULT°16 小时前
Docker 从 0 入门|吃透容器生态、架构与 Dockerfile 实战
docker·容器·架构
putItInYourHand18 小时前
Docker Compose 部署 Mosquitto MQTT Broker 完整实战指南
运维·docker·容器
生活爱好者!1 天前
哇!影视新玩法!docker部署NEOWATCH NAS
运维·docker·容器