[英]Airflow 1.10.15 dynamic task creation
我正在嘗試創建一個 DAG,它將根據上一個任務的結果生成 N 個任務。 問題是我無法在 Operator 之外使用上一個任務(在 XCom 中)返回的值
有沒有辦法使這項工作?
with DAG(
"spawn_dag",
start_date=datetime(2022, 1, 1)
) as dag:
# Calculates the number of tasks based on some previous task run
count_number_of_tasks = PythonOperator(
task_id='count_number_of_tasks',
python_callable=count_tasks_function,
dag=dag,
xcom_push=True,
provide_context=True
)
# Generates tasks and chains them
def dynamic_spawn_func(parent_dag_name, child_dag_name, start_date, args, **kwargs):
subdag = DAG(
dag_id=f"{parent_dag_name}.{child_dag_name}",
default_args=args,
start_date=start_date,
schedule_interval=None
)
# Here is the problem, the following variable cannot be used in a loop to spawn tasks
number_of_tasks = kwargs['ti'].xcom_pull(dag_id='spawn_dag', task_ids='count_number_of_tasks')
# This is where that variable is used
for j in range(number_of_tasks):
task = PythonOperator(
task_id='processor_' + str(j),
python_callable=some_func,
op_kwargs={"val": j},
dag=subdag,
provide_context=True)
task_2 = PythonOperator(
task_id='wait_for_processor_' + str(j),
python_callable=some_func,
op_kwargs={"val": j},
dag=subdag,
provide_context=True)
task >> task_2
return subdag
dynamic_spawn_op = SubDagOperator(
task_id='dynamic_spawn',
subdag=dynamic_spawn_func("spawn_dag", "dynamic_spawn", dag.start_date, args=default_args),
dag=dag,
provide_context=True
)
generate >> count_number_of_tasks >> dynamic_spawn_op
不,遷移到 Airflow 2.3+。 Airflow 1.10 已經停產 2 年了,不升級是搬起石頭砸自己的腳。 您不僅缺乏新功能(如動態任務映射),而且使自己極易受到潛在安全問題的攻擊(自 1.10 以來修復了 10 多個 CVE),而且您還把自己置於這個 position 中:
因為你是世界上最后運行 Airflow 1.10 的人之一。
在此階段不升級是非常錯誤的決定,因為不升級比遷移成本要多得多。 多次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.