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()
相关推荐
BullSmall6 小时前
Anolis OS 8.10 Docker 部署 PostgreSQL 15 完整实操
docker·postgresql·容器
java_logo10 小时前
Docker 部署 CUPS Web:浏览器即可管理打印机与打印任务
docker·容器·打印机·轩辕镜像·cups web·cups web 镜像部署·cups web 部署教程
数据知道12 小时前
容器安全入门:Docker 逃逸原理与 5 个经典 CVE
安全·网络安全·docker·容器
run21professional13 小时前
Windows 11 + WSL + Docker Desktop 开发环境设置
运维·docker·容器
giaming02315 小时前
告别Docker Desktop!一款轻量级原生开发环境管理工具:FlyEnv
运维·docker·容器
自强的小白16 小时前
Docker命令
java·docker·容器
暗影凋落18 小时前
docker-image 工具展示更详细镜像层内容
运维·docker·容器
极客先躯18 小时前
高级java每日一道面试题-2026年05月11日-实战篇[Docker]-如何容器化金融产品推荐系统?
java·运维·docker·容器·金融·高级面试·金融产品推荐系统
江湖有缘20 小时前
Docker实战 : 使用Docker部署Musicn音乐下载工具
docker·容器·eureka
沉迷学习 日益消瘦20 小时前
17-Custom Resource 入门
docker·容器·kubernetes