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()
相关推荐
VIP_CQCRE7 小时前
给团队搭一个 AI 工作台:Open WebUI 接入 Ace Data Cloud 实战
docker·ai·rag·open webui·ace data cloud
程序员老赵11 小时前
Docker 部署 Portainer CE:轻松搭建容器管理平台
docker·容器·开源
TTATTC11 小时前
Gmail、Drive、日历共用一套命令:gws 如何在运行时把 Discovery 文档编译成 CLI
docker·开源
大侠归来12 小时前
Docker 部署 Dolibarr:从零开始的完整指南
运维·docker·容器
七月吃橘子12 小时前
WSL2+Docker+Ollama,本地部署大模型实践(Qwen1.8b)
运维·docker·容器
做运维的阿瑞13 小时前
Docker常见故障全方位排查指南
运维·docker·云原生·容器
木子欢儿13 小时前
Zabbix 7.4 Alpine 版 Docker Compose 部署
android·运维·docker·容器·zabbix
打工仔折腾 AI14 小时前
极空间用 Docker 部署 Photopea:网页版 PS 的私有化实践
运维·后端·python·docker·容器·langchain·ai agent 实战
工具人555514 小时前
Docker 最新超详细版教程
运维·docker·容器
Wang's Blog14 小时前
Java 项目部署之 Docker工具快速入门: 使用 Docker 镜像命令练习
java·docker