1
votes

How does the task_ids work when multiple tasks are specified?

In this particular code example I expected to retreive the load_cycle_id_2 from both tasks in a tuple (5555,22222) but instead it comes out (None, 22222).

Why is that?

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
You are missing task that uses another_push_load_id : push_operator_1 = PythonOperator(task_id='push_load_id1', python_callable=another_push_load_id, dag=demo_dag) - kaxil
...of course! looking at it too long i guess! - dirtyw0lf

1 Answers

0
votes

Your dags runs only push_load_id andpull_load_id functions. You do not create an operator that uses another_push_load_id function.

The end of your code should look like:

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