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()
相关推荐
小新同学^O^2 小时前
简单学习 --> Docker容器
学习·docker·容器
emiya_saber2 小时前
docker标签
运维·docker·容器
毋语天2 小时前
Docker 环境下 Milvus 向量数据库的稳定部署与常见问题
数据库·docker·milvus
墨倾许2 小时前
从零搭建运维学习环境(2026):WSL + Docker + Git + VS Code 完全指南
运维·git·学习·docker·容器·bash
炸炸鱼.2 小时前
Docker 高级管理 ——Dockerfile 镜像制作
docker·镜像制作
STAT abil2 小时前
docker离线安装及部署各类中间件(x86系统架构)
docker·中间件·系统架构
极客先躯2 小时前
高级java每日一道面试题-2025年12月06日-实战篇[Dockerj]-如何配置 Docker 的镜像加速器?国内有哪些常用加速源?
java·docker·配置docker的镜像加速器·国内有哪些常用加速源·镜像加速器的本质与配置原理·镜像拉取流程对比·加速前后架构差异
kiku18182 小时前
Docker高级管理--Dockerfile镜像制作
java·docker·eureka
bukeyiwanshui2 小时前
20260509 Dockerfile案例
linux·运维·docker