Repository navigation
Add user-facing docs for the new Task Loops feature #74274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
airflow-core/docs/authoring-and-scheduling/examples/example_task_loops.py
|
ashb marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Repeat task groups with fixed counts, runtime conditions, and mapped tasks.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| # [START refine_estimate] | ||
| from airflow.sdk import dag, task, task_group | ||
|
|
||
|
|
||
| @dag(schedule=None, catchup=False, tags=["example"]) | ||
| def refine_estimate(): | ||
| @task_group | ||
| def refine(): | ||
| @task | ||
| def improve(*, loop): | ||
| estimate = 1.0 if loop.index == 0 else loop.previous["estimate"] | ||
| return (estimate + 2.0 / estimate) / 2.0 | ||
|
|
||
| @task | ||
| def evaluate(estimate): | ||
| return {"estimate": estimate, "error": abs(estimate * estimate - 2.0)} | ||
|
|
||
| evaluate(improve()) | ||
|
|
||
| def accurate_enough(*, loop): | ||
| return loop.result["error"] < 0.000001 | ||
|
|
||
| @task | ||
| def finished(): | ||
| print("Refinement finished.") | ||
|
|
||
| refinement = refine.loop(max_iterations=10, until=accurate_enough) | ||
| refinement >> finished() | ||
|
|
||
|
|
||
| refine_estimate() | ||
| # [END refine_estimate] | ||
|
|
||
|
|
||
| # [START fixed_loop] | ||
| @dag(schedule=None, catchup=False, tags=["example"]) | ||
| def fixed_task_loop(): | ||
| @task_group | ||
| def accumulate(): | ||
| @task | ||
| def increment(*, loop): | ||
| return (0 if loop.index == 0 else loop.previous) + 1 | ||
|
|
||
| increment() | ||
|
|
||
| accumulate.loop(max_iterations=3) | ||
|
|
||
|
|
||
| fixed_task_loop() | ||
| # [END fixed_loop] | ||
|
|
||
|
|
||
| # [START mapped_loop] | ||
| @dag(schedule=None, catchup=False, tags=["example"]) | ||
| def mapped_task_loop(): | ||
| @task_group | ||
| def process_batch(): | ||
| @task | ||
| def choose_items(*, loop): | ||
| return [1, 2] if loop.index == 0 else [value + 1 for value in loop.previous] | ||
|
|
||
| @task | ||
| def process(value, *, loop, ti): | ||
| print(f"Iteration {loop.index}, mapped position {ti.map_index}") | ||
| return value + loop.index | ||
|
|
||
| process.expand(value=choose_items()) | ||
|
|
||
| def batch_ready(*, loop): | ||
| return min(loop.result) >= 2 | ||
|
|
||
| process_batch.loop(max_iterations=3, until=batch_ready) | ||
|
|
||
|
|
||
| mapped_task_loop() | ||
| # [END mapped_loop] | ||
|
|
||
|
|
||
| # [START partial_override_loop] | ||
| @dag(schedule=None, catchup=False, tags=["example"]) | ||
| def partial_override_loop(): | ||
| @task_group | ||
| def accumulate(increment_by): | ||
| @task | ||
| def increment(increment_by, *, loop): | ||
| return (0 if loop.index == 0 else loop.previous) + increment_by | ||
|
|
||
| increment(increment_by) | ||
|
|
||
| accumulate.override(group_id="accumulate_more").partial(increment_by=5).loop(max_iterations=3) | ||
|
|
||
|
|
||
| partial_override_loop() | ||
| # [END partial_override_loop] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
airflow-core/docs/authoring-and-scheduling/loops-and-mapped-tasks.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| .. Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| .. http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| .. Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
|
|
||
| .. _loops-and-mapped-tasks: | ||
|
|
||
| ====================== | ||
| Loops and mapped tasks | ||
| ====================== | ||
|
|
||
| A Dag does not have to be static. How much work it does can depend on data that only exists at | ||
| runtime: how many files arrived, what the previous step found, or whether an answer is good enough | ||
| yet. Airflow has two features for this, as well as mechanisms like ``retries`` and dynamic Dag generation that fit adjacent use cases. | ||
|
|
||
| Choose a mechanism | ||
| ================== | ||
|
|
||
| .. list-table:: | ||
| :header-rows: 1 | ||
| :widths: 30 30 40 | ||
|
|
||
| * - You want to | ||
| - Use | ||
| - How it works | ||
| * - Run the same task once for each item in a collection | ||
| - :ref:`Mapped tasks <mapped-tasks>` | ||
| - ``task.expand(...)`` creates one task instance per item once the collection is known. The | ||
| instances can run concurrently. | ||
| * - Repeat a group of tasks, each iteration building on the result of the previous one, until a | ||
| condition is met | ||
| - :ref:`Loops <loops>` | ||
| - ``task_group.loop(...)`` creates the next iteration of task instances only if the last iteration says | ||
| another is needed. | ||
| * - Give a failed task another try | ||
| - Retries, set with ``retries`` on the task. See :doc:`/core-concepts/tasks`. | ||
| - A retry reruns the same task instance. It does not create new task instances, and it does not advance a | ||
| loop. | ||
| * - Create tasks from something known when the Dag file is parsed | ||
| - A Python ``for`` loop in the Dag file. See :doc:`/howto/dynamic-dag-generation`. | ||
| - The Dag has the same shape in every run. | ||
|
|
||
| Use them together | ||
| ================= | ||
|
|
||
| An iteration of a loop can contain mapped tasks, so each iteration can fan out over a different collection of | ||
| items. See :ref:`Mapped tasks inside a loop <loops-mapped-tasks>`. | ||
|
|
||
| Mapping a whole loop, nesting loops, and placing a loop inside a mapped task group are not supported. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.