ana-carolina-januario commented on issue #27785:
URL: https://github.com/apache/airflow/issues/27785#issuecomment-1320574347

   The complete code is:
   
   ```
   from __future__ import annotations
   
   from datetime import timedelta, datetime
   
   import pendulum
   from airflow import DAG
   from airflow.operators.bash import BashOperator
   from airflow.operators.python import PythonOperator
   from airflow.operators.subdag import SubDagOperator
   
   DAG_NAME = 'test_xcom'
   SUB_DAG_NAME = "subdag_writer"
   FULL_SUB_DAG_NAME = f'{DAG_NAME}.{SUB_DAG_NAME}'
   SUB_DAG_PUSH_TASK = "push_xcom_value_sub_dag"
   SUB_DAG_KEY = "key_test"
   
   
   def sub_dag(parent_dag_name, child_dag_name, args) -> DAG:
       dag_sub_dag = DAG(
           dag_id=f"{parent_dag_name}.{child_dag_name}",
           default_args={"retries": 2},
           start_date=pendulum.datetime(2022, 11, 17, tz="UTC"),
           catchup=False,
           schedule="@daily"
       )
   
       def extract(**kwargs):
           ti = kwargs["ti"]
           data_string = '{"1001": 301.27, "1002": 433.21, "1003": 502.22}'
           ti.xcom_push(key=SUB_DAG_KEY, value=data_string)
           return "xcom_value_test"
   
       PythonOperator(
           task_id=SUB_DAG_PUSH_TASK,
           default_args=args,
           python_callable=extract,
           dag=dag_sub_dag,
           do_xcom_push=True
       )
   
       return dag_sub_dag
   
   
   DEFAULT_ARGS = {"retries": 2}
   
   
   with DAG(
           dag_id=DAG_NAME,
           default_args={"retries": 2},
           start_date=datetime(2022, 1, 1),
           schedule="@once"
   ) as dag:
       def push_xcom_value(**kwargs):
           ti = kwargs['ti']
           return "test_value"
       push_xcom_value = PythonOperator(
           task_id='push_xcom_value',
           python_callable=push_xcom_value,
       )
   
       command_read_xcom_sub_dag = 'echo The returned_value xcom is {{ 
task_instance.xcom_pull(' \
                 f'task_ids="{SUB_DAG_PUSH_TASK}", ' \
                 f'dag_id="{DAG_NAME}.{SUB_DAG_NAME}"' \
                 ')}}'
       read_xcom_sub_dag = BashOperator(
           task_id="read_xcom_sub_dag",
           bash_command=command_read_xcom_sub_dag,
           do_xcom_push=False,
           dag=dag
       )
   
       command_read_xcom_bash = 'echo The returned_value xcom is {{ 
task_instance.xcom_pull(' \
                 'task_ids="push_xcom_value", ' \
                 f'dag_id="{DAG_NAME}"' \
                 ')}}'
       read_xcom_bash = BashOperator(
           task_id="read_xcom_bash",
           bash_command=command_read_xcom_bash,
           do_xcom_push=False,
           dag=dag
       )
   
       def read_xcom(**kwargs):
           ti = kwargs['ti']
           print('ti= ', ti,'\n')
           total_value_string = ti.xcom_pull(
               task_ids="push_xcom_value",
               key="return_value",
               dag_id=f"{DAG_NAME}")
           print(total_value_string)
       read_xcom_python = PythonOperator(
           task_id='read_xcom_python',
           python_callable=read_xcom,
       )
   
       def read_xcom_python_sub_dag(**kwargs):
           ti = kwargs['ti']
           total_value_string = ti.xcom_pull(
               task_ids=SUB_DAG_PUSH_TASK,
               key="return_value",
               dag_id=f"{FULL_SUB_DAG_NAME}")
           print(total_value_string)
           test = ti.xcom_pull(
               key=SUB_DAG_KEY,
               task_ids=SUB_DAG_PUSH_TASK,
               dag_id=f"{FULL_SUB_DAG_NAME}")
           print(total_value_string)
       read_xcom_python_sub_dag_task = PythonOperator(
           task_id='read_xcom_python_sub_dag',
           python_callable=read_xcom_python_sub_dag,
       )
   
       bash_push_sub_dag = SubDagOperator(
           task_id=SUB_DAG_NAME,
           subdag=sub_dag(
               parent_dag_name=DAG_NAME,
               child_dag_name=SUB_DAG_NAME,
               args=DEFAULT_ARGS),
           dag=dag
       )
   
       command_read_xcom_bash_1 = 'echo The returned_value xcom is {{ 
task_instance.xcom_pull(' \
                 f'task_ids="{SUB_DAG_PUSH_TASK}", ' \
                 f'dag_id="{DAG_NAME}"' \
                 ')}}'
       read_xcom_bash_1 = BashOperator(
           task_id="read_xcom_bash_1",
           bash_command=command_read_xcom_bash_1,
           do_xcom_push=False,
           dag=dag
       )
   
       push_xcom_value >> bash_push_sub_dag >> [read_xcom_sub_dag, 
read_xcom_bash, read_xcom_python, read_xcom_python_sub_dag_task, 
read_xcom_bash_1]
   ```
   As you can see we've tested in multiple ways to get to the subdag's pushed 
value. All with no success.
   
   Thanks in advance.


-- 
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]

Reply via email to