
Prefect 调研:23K stars 的 Python workflow orchestration,Apache-2.0 + 8 年老牌怎么跟 Airflow 拼刺刀
Prefect 是 “Python 原生现代化 workflow orchestration” —— Apache-2.0 / 23,707 ⭐ / Python / 8 年老牌 / v3.8.4 节奏活跃。Prefect 不写 DAG 文件 (不用 Airflow 的 Python DAG 抽象), 直接用 @flow + @task 装饰器 定义 workflow。本文把 Prefect 跟 Airflow / Temporal / Dagster 4 家伙伴横评一次讲透。
写在前面:编排层的 4 维赛道
打开任何一个 Python 项目想加 workflow 调度,选项至少 4 个:
| 项目 | Stars | 范式 | 主语言 | 创建 | WP |
|---|---|---|---|---|---|
| Apache Airflow | 47K ⭐ | DAG-based 批处理调度 | Python | 2015 | 306 |
| Prefect | 23K ⭐ | Python-native + dynamic DAG | Python | 2018 | 本次 |
| Dagster | 16K ⭐ | Asset-centric 编排 | Python | 2018 | 390 |
| Temporal | 22K ⭐ | 持久执行 runtime | Go + 4 SDK | 2019 | 430 |
Prefect 跟 Airflow 是 ” 现代化挑战 ” 关系 —— 都是 Python-first 调度,但 Prefect 抛弃了 Airflow 的 DAG 文件范式,用 Python 函数装饰器直接定义 workflow。
一、Prefect 是什么
基本信息
| 项 | 数据 |
|---|---|
| 仓库 | PrefectHQ/prefect |
| Stars | 23,707 ⭐(GitHub API 实时) |
| Forks | 2,485 |
| License | Apache-2.0 ✅(跟 Airflow 同 license) |
| 创建 | 2018-06-29(8 年老牌,比 Temporal 还早 1 年) |
| 最新 release | v3.8.4(2026-08-25) |
| dev 版本 | 3.8.5.dev2(2026-08-28,昨晚) |
| 最后 push | 2026-08-28(昨天,仍活跃) |
| Open issues | 861(跟 Temporal 931 接近,enterprise 阶段) |
| 主语言 | Python(原生 Python,无 Go / 其他依赖) |
| 体积 | 236 MB |
| 官网 | prefect.io |
| Topics | 14 个:python / workflow / orchestration / data-pipeline / observability / ml-ops 等 |
核心架构 :Prefect = OSS 框架 + Prefect Cloud(商业版) 双轨。OSS 是 Apache-2.0, 商用完全无风险;Cloud 提供 UI / 调度 / 多租户。
配套生态(PrefectHQ org)
| 仓库 | Stars | 定位 |
|---|---|---|
| PrefectHQ/prefab | 604 ⭐ | 🎨 Generative UI framework that even humans can build |
| PrefectHQ/colin | 122 ⭐ | Context engine treats skills as software |
| PrefectHQ/fastmcp-ts | 55 ⭐ | FastMCP TypeScript library |
| PrefectHQ/prefect-mcp-server | 52 ⭐ | Prefect MCP server(对接 AI Agent 生态) |
| PrefectHQ/terraform-prefect-aci-worker | 0 ⭐ | Terraform 部署到 Azure |
关键发现:PrefectHQ 2026 年开始押注 AI Agent + MCP 生态(prefect-mcp-server + colin + fastmcp-ts),跟 P19 循环工程横评里的 AI Agent 趋势呼应。
二、Prefect 6 大核心能力
1️⃣ Python-Native(用装饰器定义 workflow)
Prefect 抛弃了 Airflow 的 ”DAG Python 文件 ” 范式,直接用 Python 装饰器:
from prefect import flow, task
import httpx
@task(retries=3, retry_delay_seconds=10)
def fetch_data(url: str) -> dict:
response = httpx.get(url)
return response.json()
@task
def process_data(raw: dict) -> dict:
return {k: v * 2 for k, v in raw.items()}
@flow(name="Daily ETL")
def etl_flow():
# 不需要 DAG 文件,函数调用就是 workflow
raw = fetch_data("https://api.example.com")
processed = process_data(raw)
save_to_db(processed)
# 直接当 Python 函数跑
etl_flow()
# 或者调度:etl_flow.serve(cron="0 3 * * *")
对比 Airflow:
| 步骤 | Airflow | Prefect |
|---|---|---|
| 定义 task | 写 PythonOperator + Python 函数 | @task 装饰器 |
| 定义 DAG | 写 DAG 文件 + >> 链接 |
@flow 装饰器 + 函数调用 |
| 调度 | DAG 文件本身 | flow.serve(cron="0 3 * * *") |
| 状态文件 | DAG Python 文件 + DAG 列表 | 不需要,代码即 DAG |
2️⃣ Dynamic DAG(运行时动态构建)
Airflow 的 DAG 是静态的 —— DAG 文件 parse 一次后整个 run 都按这个结构跑。
Prefect 的 workflow 是动态的 —— 每次 run 都重新构建:
@flow
def dynamic_pipeline():
# 运行时根据数据决定下一步
if config["source"] == "bigquery":
fetch_from_bigquery()
elif config["source"] == "snowflake":
fetch_from_snowflake()
# 运行时循环
for table in tables_to_process:
process_table(table) # 动态生成 N 个 task run
对比 Airflow:Airflow 跑多 table 需要写循环 + DAG 模板,Prefect 直接用 Python 循环 + task 装饰器即可。
3️⃣ Async-First(原生 async/await)
Prefect 原生支持 async:
@task
async def fetch_async(url: str) -> dict:
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.json()
@flow
async def async_pipeline():
# 并发跑多个 async task
results = await asyncio.gather(fetch_async(url1),
fetch_async(url2),
fetch_async(url3),
)
对比 Airflow:Airflow 的 async 支持是 ” 附加 ”(要绕一圈),Prefect async 是 一等公民。
4️⃣ Self-Healing(自愈能力)
Prefect 自动处理 workflow 失败:
- ✅ 自动重试:每个 task 可以配
retries=3, retry_delay_seconds=10 - ✅ 失败缓存 :
cache_key_fn缓存失败结果, 不浪费 retry - ✅ 超时控制:每个 task / flow 配超时
- ✅ 失败回调:
on_failure/on_completion钩子 - ✅ 状态恢复:worker 崩溃后重启,自动从失败点继续
对比 Airflow:Airflow task 失败后 retry 是 task-level(从 task 起点重跑),Prefect retry 可以更细粒度(缓存 + 增量 retry)。
5️⃣ Observability(可观测性)
Prefect OSS 自带 UI(Prefect UI 430 ⭐):
- ✅ Flow run 历史
- ✅ Task run 详情 + 日志
- ✅ Schedule + 调度器
- ✅ Work pool 管理
- ✅ Artifact 跟踪(每个 run 的自定义元数据)
Prefect Cloud 额外提供:
– ✅ 多租户 RBAC
– ✅ Audit log
– ✅ SSO + 团队协作
– ✅ Webhook 触发
对比 Airflow:Airflow UI 更成熟(10+ 年沉淀),Prefect UI 更现代(2024+ 重新设计)。
6️⃣ Hybrid Cloud(OSS + Cloud 双轨)
Prefect 是少数同时跑 OSS + Cloud 双轨的 workflow 引擎:
| 能力 | OSS(Apache-2.0) | Prefect Cloud(商业) |
|---|---|---|
| 核心调度 | ✅ | ✅ |
| Self-host UI | ✅ | ✅ |
| 团队协作 | ⚠️ 单租户 | ✅ 多租户 RBAC |
| SSO | ❌ | ✅ |
| Audit log | ❌ | ✅ |
| Webhook trigger | ⚠️ 基础 | ✅ 高级 |
| 上云迁移 | 需重部署 | 一鍵迁移 |
对比 Temporal:Temporal Cloud 也类似,两家商业策略高度相似 —— OSS 80% + Cloud 20% enterprise 加成。
三、Prefect vs Airflow vs Temporal vs Dagster 4 家伙伴对比
12 维深度对比表
| 维度 | Prefect | Apache Airflow | Temporal | Dagster |
|---|---|---|---|---|
| License | Apache-2.0 ✅ | Apache-2.0 ✅ | MIT ✅ | Apache-2.0 ✅ |
| Stars | 23,707 ⭐ | 46,609 ⭐(2 倍 Prefect) | 22,584 ⭐ | 16,065 ⭐ |
| 创建年份 | 2018(8 年) | 2015(11 年) | 2019(7 年) | 2018(8 年) |
| 范式 | Python decorator + 动态 DAG | DAG Python 文件 | Workflow as Code | Asset-centric |
| 主语言 | Python | Python | Go + 4 SDK | Python |
| 学习曲线 | 低(@flow / @task 装饰器) | 中(DAG 文件 + operator 抽象) | 中(determinism 约束) | 中(asset + op 双概念) |
| 动态 DAG | ✅ 原生支持 | ❌ 静态(DAG 文件 parse 一次) | N/A(workflow 是代码) | ⚠️ 部分支持 |
| Async 原生 | ✅ 一等公民 | ⚠️ 需配置 | ✅ SDK 支持 | ✅ 支持 |
| 持久执行 | ❌ | ❌ | ✅ 核心 | ❌ |
| 生态成熟度 | 中(崛起中) | 高(70+ providers) | 中(4 SDK) | 中(dagster-cloud) |
| AI Agent 集成 | ✅ prefect-mcp-server | ⚠️ 第三方 | ⚠️ 第三方 | ⚠️ 第三方 |
| 最大卖点 | Python-native 现代化 | 生态成熟 + 70+ providers | 持久执行 + sub-second dispatch | 资产 lineage + 软件定义资产 |
核心差异
1️⃣ Python-Native 现代化 vs 老牌 DAG(Prefect vs Airflow)
Airflow 痛点(飞熊读者最熟悉的):
– DAG 文件 parse 时间长(大型项目可能 1+ 分钟)
– DAG Python 文件是 ” 另一种语言 ”(operator 抽象)
– 静态 DAG,动态场景需要 workaround
Prefect 解决:
– 装饰器语法 = 普通 Python 函数(没有新概念)
– 动态 DAG 天然支持(Python 循环 + task 装饰器)
– 不需要 separate DAG 文件
但 Airflow 仍然胜在:70+ providers + 10+ 年生态 + 文档齐全 —— 大型企业数据栈迁移成本高。
2️⃣ Workflow Orchestration vs Durable Execution(Prefect vs Temporal)
Prefect:跑 Python task,状态由 Prefect server 管理,重启后会重跑整个 flow
Temporal:workflow 状态自动持久化,重启后从崩溃点继续(不需要重跑)
简单说:
– Prefect =
Task runner(让 Python task 跑起来 + 调度)
– Temporal =
Workflow runtime(让长程 workflow 状态不丢)
对比维度:
| 场景 | Prefect | Temporal |
|---|---|---|
| 短任务(<1 小时) | ✅ 完美 | ⚠️ 过重 |
| 长任务(小时 - 天 - 周) | ⚠️ flow 状态可能丢 | ✅ 持久执行 |
| Saga pattern | ⚠️ 需手工实现 | ✅ SDK 内置 |
| 时间旅行调试 | ❌ | ✅ |
3️⃣ Asset-Centric vs Task-Centric(Prefect vs Dagster)
Dagster 哲学:“Asset is the source of truth” —— 你先定义数据资产(data asset),Dagster 自动算依赖 + 调度
Prefect 哲学:“Task is the source of truth” —— 你定义 task 函数,Prefect 调度
对比:
– Dagster = 适合
数据 lineage 重要的场景(金融 / 医疗)
– Prefect = 适合
Python 数据科学 / ML 流水线
四、6 大场景决策矩阵
| 场景 | 推荐 | 理由 |
|---|---|---|
| 📊 批处理数据管道(每日 ETL / 大数据) | Airflow | 70+ providers + DAG 模型成熟 + backfill + 数据间隔感知 |
| 🐍 Python 数据科学 / ML pipeline | Prefect ⭐ | Python-native 装饰器 + 动态 DAG + async 原生 |
| 🏢 DAG + 70+ operators 生态 | Airflow | 老牌 + 文档齐全 + 团队熟悉度高 |
| ⏱️ 长程应用工作流(订单 / 订阅) | Temporal | 持久执行 + sub-second dispatch |
| 🔄 数据资产 lineage(金融 / 合规) | Dagster | Asset-centric + 软件定义资产 + lineage 完整 |
| 🤖 AI Agent 长程任务 | LoopX | 8 host 集成 + evidence + quota 概念(专用) |
核心判断 : 选 Prefect 的核心场景 = “Python 工程师想用最少摩擦跑 workflow”。
五、Prefect 3.x 重大升级(vs Prefect 2.x)
Prefect 3.x 是 2024 年发布的重大重构 ,跟 2.x 是 部分不兼容:
| 特性 | Prefect 2.x | Prefect 3.x |
|---|---|---|
| 核心定位 | Hybrid execution engine | Workflow orchestration framework |
| Task runner | Dask / Ray / K8s | Subprocess / Dask / K8s(更简洁) |
| Engine | Orion engine | Pydantic 2 + async-native |
| Web UI | Reactive | 更现代化 + 快 |
| Prefect Cloud 集成 | 深度 | 更轻量 + 可选 |
| breaking changes | — | ✅ 有(从 2.x 升级需要迁移) |
Prefect 3.x 主要解决了 2.x 的痛点:
– 2.x 的 Orion engine 太复杂
– 2.x 部署难(Dask / K8s 都要单独配)
– 3.x 简化部署:本地 subprocess 就能跑
六、35 分钟 5 步实战:跑通 Prefect ETL
Step 1:装 Prefect(2 分钟)
pip install prefect
Step 2:写 ETL flow(10 分钟)
# etl_flow.py
from prefect import flow, task
from prefect.tasks import exponential_backoff
import httpx
import pandas as pd
from sqlalchemy import create_engine
@task(retries=3, retry_delay_seconds=exponential_backoff(backoff_factor=10))
def extract() -> pd.DataFrame:
""" 从 GitHub API 拉 PR 数据 """
response = httpx.get("https://api.github.com/repos/PrefectHQ/prefect/pulls?state=closed&per_page=100")
data = response.json()
return pd.DataFrame([{"number": pr["number"],
"title": pr["title"],
"user": pr["user"]["login"],
"merged_at": pr["merged_at"],
"additions": pr["additions"],
"deletions": pr["deletions"],
} for pr in data])
@task
def transform(df: pd.DataFrame) -> pd.DataFrame:
""" 转换:算每用户 PR 总数 """
return df.groupby("user").agg(pr_count=("number", "count"),
total_additions=("additions", "sum"),
total_deletions=("deletions", "sum"),
).reset_index()
@task
def load(df: pd.DataFrame):
""" 加载到 DuckDB"""
engine = create_engine("duckdb:///prefect-etl.db")
df.to_sql("user_pr_stats", engine, if_exists="replace", index=False)
print(f"✅ Loaded {len(df)} rows to DuckDB")
@flow(name="GitHub PR Stats")
def etl_flow():
raw = extract()
processed = transform(raw)
load(processed)
if __name__ == "__main__":
etl_flow()
Step 3:本地跑 flow(3 分钟)
python etl_flow.py
输出:
12:34:56.789 | INFO | Flow run 'ancient-quetzal' begins...
12:34:57.123 | INFO | Task run 'extract-abc' - Finished in state Completed
12:34:57.456 | INFO | Task run 'transform-xyz' - Finished in state Completed
12:34:57.789 | INFO | Task run 'load-mno' - Finished in state Completed
12:34:57.890 | INFO | Flow run 'ancient-quetzal' - Finished in state Completed
✅ Loaded 87 rows to DuckDB
Step 4:调度(10 分钟)
# 启动 Prefect server(自带 UI)prefect server start --host 0.0.0.0
# UI 在 http://localhost:4200
# 另一个 terminal: 让 flow 每天 3 AM 跑
python etl_flow.py # 必须先 import 一次
python -c "
from prefect import deploy
from etl_flow import etl_flow
etl_flow.serve(name='daily-pr-stats', cron='0 3 * * *')
"
Step 5:监控 + 重试(10 分钟)
访问 http://localhost:4200:
– Flows → 看到
daily-pr-stats 已调度
– 点击 flow run → 看 task 列表 + 每个 task 的日志 + retry 次数
– Work Pools → 配置 worker 池(Docker / K8s / Subprocess)
七、6 条风险清单
⚠️ 风险 1:861 open issues 偏多
23,707 stars + 861 open issues(issue/stars 比 3.6%,行业平均 0.5-1%)。社区响应可能滞后,企业生产环境锁定前先观察。
⚠️ 风险 2:3.x 升级 breaking changes 多
Prefect 2.x → 3.x 有 breaking changes,老项目升级需要迁移工作。
⚠️ 风险 3:OSS 部分企业级功能需要 Cloud
- ✅ OSS:核心调度 + UI + artifact 跟踪
- ⚠️ Cloud:多租户 RBAC + SSO + audit log + webhook
如果企业需要 RBAC / SSO,OSS 满足不了,必须付费 Cloud。
⚠️ 风险 4:生态成熟度不如 Airflow
Airflow 有 70+ providers(Snowflake / BigQuery / dbt / Spark / Kubernetes),Prefect 的 integration library 还在补齐中(prefect-aws / prefect-gcp / prefect-azure / prefect-k8s 等)。
⚠️ 风险 5:dynamic DAG 调试困难
虽然 dynamic DAG 是 Prefect 优势,但 @task 装饰器函数嵌套太深时,traceback 调试困难。
⚠️ 风险 6:Cloud 锁定 + 商业压力
PrefectHQ 是商业公司,Prefect Cloud 是核心收入来源。未来 OSS 版本可能功能降级以推 Cloud(类似 GitLab / Grafana 的 Open Core 路径)。
八、跟已有调研的关系
| 项目 | WP ID | 定位 | 与 Prefect 关系 |
|---|---|---|---|
| Apache Airflow | 306 | DAG-based 批处理调度 | 最大对手(同 Python 同调度) |
| Temporal | 430 | 持久执行 runtime | 范式不同(workflow 持久 vs task 调度) |
| Dagster | 390 | Asset-centric 编排 | 同期同 Python(不同哲学) |
| Celery | 288 | Python task queue | 更底层(Prefect 可以底层用 Celery) |
| Dagster 调研 | 390 | 同 Python 同 8 年 | 同期 8 年(不同哲学) |
| dlt+Prefect+dbt 三件套实战 | 417 | Prefect 唯一入镜 | 首次以配角出现 |
| LoopX | 403 | AI Agent 长程状态控制面 | 通用 vs AI 专用 |
| 循环工程 4 件套 | 428 | AI Agent 循环层 | AI 专用 vs 通用 workflow |
关键判断:
Prefect 跟 Airflow 是 ” 现代化挑战 ” 关系,跟 Temporal 是 ” 范式分支 ” 关系,跟 Dagster 是 ” 哲学分歧 ” 关系 。 对 Python 工程师来说,Prefect 是 2026 年的最佳起点 (学习曲线低 + Apache-2.0 + 现代 API), 如果业务量大再迁移到 Airflow 70+ providers 生态或 Temporal 持久执行 runtime。
九、总结
3 个最值得用 Prefect 的理由
1️⃣ Python-Native 装饰器 = 学习曲线最低
用普通 Python 函数定义 workflow,没有 DAG 文件、没有 operator 抽象、没有新概念。Python 工程师 1 小时就能上手。
2️⃣ Apache-2.0 + 8 年老牌 + 23K stars
跟 Airflow 同样 Apache-2.0 license,比 Temporal 早 1 年创建,商业完全无风险。v3.8.4 节奏活跃(2026-08-25 release)。
3️⃣ Dynamic DAG + Async-First + MCP server
运行时动态构建 workflow(Airflow 不支持)+ async/await 原生支持(不是附加)+ prefect-mcp-server 2026 押注 AI Agent 生态。
不适合用 Prefect 的场景
- ❌ 企业大数据 ETL(用 Airflow,70+ providers + 10+ 年生态)
- ❌ 长程应用工作流(用 Temporal,持久执行 + sub-second dispatch)
- ❌ 数据资产 lineage(用 Dagster,asset-centric + 软件定义资产)
- ❌ AI Agent 专用(用 LoopX,evidence + quota 概念)
先试一周
| Day | 任务 |
|---|---|
| 1 | 装 Prefect + 写一个最简单的 @flow + @task 函数 |
| 2-3 | 跑通 ETL flow(extract + transform + load 三个 task) |
| 4-5 | 启动 Prefect server + UI,看 flow run 历史 |
| 6-7 | 加调度(cron)+ 重试策略 + artifact 跟踪 |
一周后如果你觉得 ”workflow 就是 Python 函数 ” 的感觉对了,恭喜你进入了 Prefect 范式。
参考
- PrefectHQ/prefect GitHub —— 主仓 23,707 ⭐ / Apache-2.0
- Prefect 官网 —— 商业版 Prefect Cloud 信息
- Prefect 官方文档 —— 最新 3.x 文档
- PrefectHQ/prefab —— 配套 Generative UI 604 ⭐
- PrefectHQ/colin —— Context engine 122 ⭐
- PrefectHQ/prefect-mcp-server —— MCP server 52 ⭐
- PrefectHQ/fastmcp-ts —— FastMCP TypeScript 55 ⭐
- PrefectHQ/terraform-prefect-aci-worker —— Terraform 部署
- Apache Airflow 调研 (WP 306) —— Prefect 最大对手
- Dagster 调研 (WP 390) —— Prefect 同 Python 同 8 年(不同哲学)
- Temporal 调研 (WP 430) —— 持久执行范式
- dlt+Prefect+dbt 三件套实战 (WP 417) —— Prefect 首次入镜
- 循环工程 4 件套横评 (WP 428) —— AI Agent 循环层
- LoopX 调研 (WP 403) —— AI Agent 专用长程控制面
📎 WordPress 链接
- 官方链接:《Prefect 调研:23K stars 的 Python workflow orchestration,Apache-2.0 + 8 年老牌怎么跟 Airflow 拼刺刀》
- 短链:
https://east196.cn/?p=432 - WordPress API ID:432
- 状态:published · 2026-08-29