ephraimbuddy commented on code in PR #22941:
URL: https://github.com/apache/airflow/pull/22941#discussion_r850557065
##########
docs/apache-airflow/tutorial_taskflow_api.rst:
##########
@@ -160,6 +160,65 @@ the dependencies as shown below.
:start-after: [START main_flow]
:end-before: [END main_flow]
+
+Re-using a decorated task
+-------------------------
+
+Decorated tasks are flexible. You can re-use a decorated task in multiple
DAGs, overriding the operator
+parameters such as the ``task_id``, ``queue``, ``pool``, etc, as well as the
task's arguments.
+
+Below is an example of how you can re-use a decorated task in multiple DAGs:
+
+.. code-block:: python
+ from airflow.decorators import task, dag
+ from datetime import datetime
+
+
+ @task
+ def add_task(x, y):
+ print(f"Task args: x={x}, y={y}")
+ return x + y
+
+
+ @dag(start_date=datetime(2022, 1, 1))
+ def mydag():
+ start = add_task.override(task_id="start")(1, 2)
+ for i in range(3):
+ start >> add_task.override(task_id=f"add_start_{i}")(start, i)
+
+
+ @dag(start_date=datetime(2022, 1, 1))
+ def mydag2():
+ start = add_task(1, 2)
+ for i in range(3):
+ start >> add_task.override(task_id=f"new_add_task_{i}")(start, i)
+
+
+ first_dag = mydag()
+ second_dag = mydag2()
+
+You can also import the above ``add_task`` and use it in another DAG file.
+Suppose the code above lives in a file called ``common.py``. You can do this:
+
+.. code-block:: python
+
+ from common import add_task
+ from airflow.decorators import dag
+ from datetime import datetime
+
+
+ @dag(start_date=datetime(2022, 1, 1))
+ def use_add_task():
+ start = add_task.override(priority_weight=3)(1, 2)
+ for i in range(3):
+ start >> add_task.override(task_id=f"new_add_task_{i}",
pool="default_pool")(
Review Comment:
Maybe I will change it to another arg. I want the code to run if copied
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]