Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow: How to pass xcom from parent dag to subdag

Tags:

python

airflow

Considering a parent dag that pushes value to xcom, how to retrieve dag from subdag?

What I've tried:

#parent_dag.py

PARENT_DAG_NAME = "MyParentDag"
CHILD_DAG_NAME = "MyChildDag"

main_dag = DAG(
  dag_id=PARENT_DAG_NAME,
  schedule_interval="@hourly",
  start_date=DAG_START_DATE
)


def push_value(**kwargs):
    ''' push into Xcom '''
    return [1, 2]

t1 = PythonOperator(task_id='push_value',
                       python_callable=push_value,
                       retries=3,
                       dag=main_dag)

subdag_1 = SubDagOperator(
  subdag=Sub_Dag1(
      PARENT_DAG_NAME,
      CHILD_DAG_NAME,
      main_dag.start_date,
      main_dag.schedule_interval,
      "'{{ ti.xcom_pull(task_ids='push_value', dag_id='" + PARENT_DAG_NAME + "' }}'"
  ),
  task_id=CHILD_DAG_NAME,
  dag=main_dag,
)
t1 >> subdag_1

And the child subdag:

#subdag1.py


def use_pushed_val(pushed_val, ds, **kwargs):
    log.info(pushed_val)
    return pushed_val

def Sub_Dag1(parent_dag_name, child_dag_name, start_date, schedule_interval, pushed_val):
  dag = DAG(
    '%s.%s' % (parent_dag_name, child_dag_name),
    schedule_interval=schedule_interval,
    start_date=start_date,
  )

  childTask = PythonOperator(
      task_id='child_task',
      python_callable=use_pushed_val,
      op_kwargs = {'pushed_val' : pushed_val},
      provide_context=True,
      dag=dag
    )

return dag

Instead of child subdag to log and return the [1,2], it returned string '{{ ti.xcom_pull(task_ids='push_value', dag_id='MyParentDag' }}'

like image 442
DougKruger Avatar asked Nov 17 '22 15:11

DougKruger


1 Answers

{{ti.xcom_pull(task_ids='push_value', dag_id='" + PARENT_DAG_NAME + "')}}

In the above code a parenthesis is missing

like image 180
RaHuL KuMaR ShArMa Avatar answered Nov 19 '22 11:11

RaHuL KuMaR ShArMa