气流,XCom和多个task_id

时间:2018-12-22 15:30:27

标签: python airflow apache-airflow-xcom

指定多个任务时task_id如何工作?

在这个特定的代码示例中,我希望从一个元组的两个任务中检索load_cycle_id_2(5555,22222),但结果却出来了(None,22222)。

那是为什么?

from airflow.models import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime

args = {
    'owner': 'airflow',
    'start_date': datetime.now(),
    'provide_context': True
}

demo_dag = DAG(dag_id='first', start_date=datetime.now(), schedule_interval='@once',default_args=args)

def push_load_id(**kwargs):
    kwargs['ti'].xcom_push(key='load_cycle_id_2',value=22222)
    kwargs['ti'].xcom_push(key='load_cycle_id_3',value=44444)

def another_push_load_id(**kwargs):
    kwargs['ti'].xcom_push(key='load_cycle_id_2',value=5555)
    kwargs['ti'].xcom_push(key='anotherload_cycle_id_3',value=6666)

def pull_load_id(**kwargs):
    ti = kwargs['ti'].xcom_pull(key='load_cycle_id_2', task_ids=['another_push_load_id','push_load_id'])
    print(ti)

push_operator = PythonOperator(task_id='push_load_id', python_callable=push_load_id, dag=demo_dag)
pull_operator = PythonOperator(task_id='pull_load_id', python_callable=pull_load_id, dag=demo_dag)

push_operator >> pull_operator

1 个答案:

答案 0 :(得分:0)

您的dags仅运行push_load_idpull_load_id函数。您没有创建使用another_push_load_id函数的运算符。

代码的结尾应如下所示:

push_operator = PythonOperator(task_id='push_load_id', python_callable=push_load_id, dag=demo_dag)
another_push_operator = PythonOperator(task_id='push_load_id', python_callable= another_push_load_id, dag=demo_dag)
pull_operator = PythonOperator(task_id='pull_load_id', python_callable=pull_load_id, dag=demo_dag)

push_operator >> another_push_operator >> pull_operator