📖 Scenario: You are working with Apache Airflow to automate tasks. You want to pass data between tasks using XComs, which allow tasks to share small pieces of information.Imagine you have two tasks: one that generates a number, and another that uses that number to print a message.
🎯 Goal: Build a simple Airflow DAG with two Python tasks. The first task returns a value using XCom. The second task reads that value and prints it.
📋 What You'll Learn
Create a Python function called generate_number that returns the integer 42.
Create a Python function called print_number that accepts ti as a parameter and pulls the XCom value from generate_number task.
Create a DAG named xcom_return_value_dag with two tasks: task_generate and task_print.
Set task_generate to run generate_number and task_print to run print_number.
Set task_generate to run before task_print.
Print the pulled XCom value inside print_number.
💡 Why This Matters
🌍 Real World
In real Airflow workflows, tasks often need to share data like IDs, statuses, or results. XComs let you pass this data easily between tasks.
💼 Career
Understanding XComs is essential for Airflow users and DevOps engineers who automate workflows and need tasks to communicate with each other.
Progress0 / 4 steps
1
Create the generate_number function
Write a Python function called generate_number that returns the integer 42.
Apache Airflow
Hint
Define a function with def and use return 42 inside it.
2
Create the print_number function to pull XCom
Write a Python function called print_number that accepts a parameter ti. Inside it, pull the XCom value from the task task_generate using ti.xcom_pull(task_ids='task_generate') and store it in a variable called number.
Apache Airflow
Hint
Use ti.xcom_pull(task_ids='task_generate') to get the value returned by generate_number.
3
Create the DAG and tasks
Import DAG and PythonOperator from Airflow. Create a DAG named xcom_return_value_dag with start_date as days_ago(1) and schedule_interval as None. Inside the DAG, create two tasks: task_generate running generate_number and task_print running print_number. Set task_generate to run before task_print.
Apache Airflow
Hint
Use PythonOperator to run your functions as tasks inside the DAG context.
4
Print the pulled XCom value in print_number
Add a print statement inside the print_number function to display the variable number.
Apache Airflow
Hint
Use print(number) to show the value pulled from XCom.
Practice
(1/5)
1. What does returning a value from a PythonOperator task do in Airflow?
easy
A. Automatically pushes the returned value to XCom for other tasks to use.
B. Saves the value only in the task logs without sharing.
C. Deletes any previous XCom values for that task.
D. Triggers the next task immediately without waiting.
Solution
Step 1: Understand PythonOperator return behavior
When a PythonOperator returns a value, Airflow automatically stores it in XCom for that task instance.
Step 2: Recognize XCom purpose
XCom is designed to share data between tasks, so returning a value pushes it to XCom for downstream tasks.
Final Answer:
Automatically pushes the returned value to XCom for other tasks to use. -> Option A
Quick Check:
Return value = XCom push [OK]
Hint: Return values from PythonOperator go to XCom automatically [OK]
Common Mistakes:
Thinking return values are only logged, not shared
Assuming return clears previous XCom data
Believing return triggers next task immediately
2. Which of the following is the correct way to pull an XCom value in a downstream task?
easy
A. ti.xcom_pull(task_ids='task1', key='return_value')
B. ti.xcom_push(key='result')
C. xcom_pull(task_id='task1')
D. ti.pull_xcom(task_id='task1')
Solution
Step 1: Identify correct method signature
The method to pull XCom data is ti.xcom_pull(), which requires task_ids and optionally a key.
Step 2: Match correct parameters
Correct usage is ti.xcom_pull(task_ids='task1', key='return_value') to get the returned value from task1.
Final Answer:
ti.xcom_pull(task_ids='task1', key='return_value') -> Option A
Quick Check:
Use ti.xcom_pull with task_ids and key [OK]
Hint: Use ti.xcom_pull(task_ids, key) to get XCom data [OK]
Common Mistakes:
Using xcom_push instead of xcom_pull to get data
Wrong parameter names like task_id instead of task_ids