Prefect 是一个用于数据密集型工作流程编排的工具。它能够最简单地将任何 Python 函数转变成一个可被观察和编排的工作单元。通过 Prefect 可以构建弹性的、动态的工作流程,使其能够对周围的环境做出反应,并能从意外变化中恢复过来。通过添加装饰器,Prefect 提供了自动重试、分布式执行、调度、缓存等诸多增强功能。每个任务都能被追踪,并可通过 Prefect 服务器或 Prefect Cloud 仪表板进行监控。
from prefect import flow, task from typing import List import httpx @task(retries=3) def get_stars(repo: str): url = f"https://api.github.com/repos/{repo}" count = httpx.get(url).json()["stargazers_count"] print(f"{repo} has {count} stars!") @flow(name="GitHub Stars") def github_stars(repos: List[str]): for repo in repos: get_stars(repo) # run the flow! github_stars(["PrefectHQ/Prefect"])
运行一些流程后,打开 Prefect UI 查看:
prefect server start
这是将任何 Python 函数转换为可观察和编排的工作单元的最简单方法。
快速入门
第 1 步:安装
pip install -U prefect
第 2 步:连接 Prefect 的 API
注册永久免费的 Prefect Cloud 帐户,或者使用 Prefect Cloud 的部分功能托管个人服务器。
-
如果使用 Prefect Cloud,请使用现有帐户登录或在
https://app.prefect.cloud/
创建新帐户。 -
如果设置新帐户,请为帐户创建一个工作区。
-
使用
prefect cloud login
Prefect CLI 命令从你的环境登录 Prefect Cloud 。
prefect cloud login