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()
相关推荐
nuo5342025 分钟前
基础 3 —— Docker 的常用命令
docker·容器
oushaojun21 小时前
deepseek教我什么是docker
运维·docker·容器
jieyucx2 小时前
Windows11 & Linux 环境Docker部署与镜像拉取效率优化实战指南
linux·运维·docker
从此以后自律2 小时前
项目 Docker + Nginx 云端完整部署细节
nginx·docker·容器
donoot19 小时前
一次 Nginx 502 问题的深度排查:从 SELinux 到容器网络
nginx·docker·selinux·反向代理·502 bad gateway
Sagittarius_A*20 小时前
Docker:渗透前置容器化核心基础
运维·安全·docker·容器
名字还没想好☜1 天前
Docker 多阶段构建:把镜像从 1.2GB 砍到 15MB
运维·docker·容器·多阶段构建·镜像优化
生活爱好者!1 天前
NAS还能玩游戏?部署一款 WebGL 3D 赛车小游戏
游戏·docker·容器·玩游戏
曦月合一1 天前
本地电脑部署docker,及各种组件(nginx、mysql、redis、mongo、minio、java)
docker·windows10 专业版
hj2862511 天前
Docker 核心知识点整理(网络 + Dockerfile + 原理 + 命令)
网络·docker·容器