Complete the code to push a value to XCom using the return statement in a PythonOperator task.
def task_function(): return [1]
Returning a value from the task function automatically pushes it to XCom.
Complete the code to pull the XCom value from a previous task named 'push_task'.
from airflow.operators.python import PythonOperator def pull_task(ti): value = ti.xcom_pull(task_ids=[1]) print(value)
Task IDs must be passed as strings to xcom_pull.
Fix the error in the code to correctly return a dictionary as an XCom value.
def task_function(): data = [1] # dictionary to return return data
Returning a dictionary pushes it as an XCom value correctly.
Fill both blanks to create a PythonOperator that pushes a return value and another that pulls it.
push_task = PythonOperator(
task_id='push_task',
python_callable=[1],
dag=dag
)
pull_task = PythonOperator(
task_id='pull_task',
python_callable=[2],
dag=dag
)The push task uses the function that returns a value, and the pull task uses the function that pulls it.
Fill all three blanks to create a DAG with two tasks where the second pulls the XCom value from the first.
from airflow import DAG from airflow.operators.python import PythonOperator from datetime import datetime def [1](): return 'Data from first task' def [2](ti): data = ti.xcom_pull(task_ids=[3]) print(f'Received: {data}') dag = DAG('xcom_example', start_date=datetime(2024, 1, 1), schedule_interval='@daily') push = PythonOperator(task_id='push', python_callable=[1], dag=dag) pull = PythonOperator(task_id='pull', python_callable=[2], dag=dag) push >> pull
The first function returns data, the second pulls it using the correct task ID string.