2025-11-13 19:47:22 +09:00

49 lines
1.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# prefectの使い方
Flow関数の中で、Prefectの @task が付いた関数を呼び出すことで、処理単位(タスク)を組み合わせて実行します。
## コンポ―ネート
### @flow
Prefectにおける「ワークフロー(全体の処理のまとまり)」
を定義するデコレータです。
Pythonの関数を「フロー関数(Flow Function)」に変えます。
```py
@flow
def etl_flow(d: str | None = None):
d = d or date.today().isoformat()
load(transform(extract(d)))
```
タスクの呼び出しががわかりにくいので分解すると以下になる
```py
@flow
def etl_flow(d=None):
d = d or date.today().isoformat()
# load(transform(extract(d)))
raw = extract(d)
clean = transform(raw)
load(clean)
```
### @task
Prefectが管理する個々の処理単位タスクを定義します。
通常のPython関数にリトライやログ管理、
依存関係管理などを付けられる。
```py
@task(retries=3, retry_delay_seconds=10)
def extract(d):
return f"raw({d})"
```
* retries:
* 最大3回リトライ
* retry_delay_seconds:
* 失敗したら10秒待って再試行という「実行単位」