|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Repeat task groups with fixed counts, runtime conditions, and mapped tasks.""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +# [START refine_estimate] |
| 22 | +from airflow.sdk import dag, task, task_group |
| 23 | + |
| 24 | + |
| 25 | +@dag(schedule=None, catchup=False, tags=["example"]) |
| 26 | +def refine_estimate(): |
| 27 | + @task_group |
| 28 | + def refine(): |
| 29 | + @task |
| 30 | + def improve(*, loop): |
| 31 | + estimate = 1.0 if loop.index == 0 else loop.previous["estimate"] |
| 32 | + return (estimate + 2.0 / estimate) / 2.0 |
| 33 | + |
| 34 | + @task |
| 35 | + def evaluate(estimate): |
| 36 | + return {"estimate": estimate, "error": abs(estimate * estimate - 2.0)} |
| 37 | + |
| 38 | + evaluate(improve()) |
| 39 | + |
| 40 | + def accurate_enough(*, loop): |
| 41 | + return loop.result["error"] < 0.000001 |
| 42 | + |
| 43 | + @task |
| 44 | + def finished(): |
| 45 | + print("Refinement finished.") |
| 46 | + |
| 47 | + refinement = refine.loop(max_iterations=10, until=accurate_enough) |
| 48 | + refinement >> finished() |
| 49 | + |
| 50 | + |
| 51 | +refine_estimate() |
| 52 | +# [END refine_estimate] |
| 53 | + |
| 54 | + |
| 55 | +# [START fixed_loop] |
| 56 | +@dag(schedule=None, catchup=False, tags=["example"]) |
| 57 | +def fixed_task_loop(): |
| 58 | + @task_group |
| 59 | + def accumulate(): |
| 60 | + @task |
| 61 | + def increment(*, loop): |
| 62 | + return (0 if loop.index == 0 else loop.previous) + 1 |
| 63 | + |
| 64 | + increment() |
| 65 | + |
| 66 | + accumulate.loop(max_iterations=3) |
| 67 | + |
| 68 | + |
| 69 | +fixed_task_loop() |
| 70 | +# [END fixed_loop] |
| 71 | + |
| 72 | + |
| 73 | +# [START mapped_loop] |
| 74 | +@dag(schedule=None, catchup=False, tags=["example"]) |
| 75 | +def mapped_task_loop(): |
| 76 | + @task_group |
| 77 | + def process_batch(): |
| 78 | + @task |
| 79 | + def choose_items(*, loop): |
| 80 | + return [1, 2] if loop.index == 0 else [value + 1 for value in loop.previous] |
| 81 | + |
| 82 | + @task |
| 83 | + def process(value, *, loop, ti): |
| 84 | + print(f"Iteration {loop.index}, mapped position {ti.map_index}") |
| 85 | + return value + loop.index |
| 86 | + |
| 87 | + process.expand(value=choose_items()) |
| 88 | + |
| 89 | + def batch_ready(*, loop): |
| 90 | + return min(loop.result) >= 2 |
| 91 | + |
| 92 | + process_batch.loop(max_iterations=3, until=batch_ready) |
| 93 | + |
| 94 | + |
| 95 | +mapped_task_loop() |
| 96 | +# [END mapped_loop] |
| 97 | + |
| 98 | + |
| 99 | +# [START partial_override_loop] |
| 100 | +@dag(schedule=None, catchup=False, tags=["example"]) |
| 101 | +def partial_override_loop(): |
| 102 | + @task_group |
| 103 | + def accumulate(increment_by): |
| 104 | + @task |
| 105 | + def increment(increment_by, *, loop): |
| 106 | + return (0 if loop.index == 0 else loop.previous) + increment_by |
| 107 | + |
| 108 | + increment(increment_by) |
| 109 | + |
| 110 | + accumulate.override(group_id="accumulate_more").partial(increment_by=5).loop(max_iterations=3) |
| 111 | + |
| 112 | + |
| 113 | +partial_override_loop() |
| 114 | +# [END partial_override_loop] |
0 commit comments