0
0
Apache Airflowdevops~10 mins

XCom with return values in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - XCom with return values
Task A runs
Task A returns value
Airflow stores value in XCom
Task B starts
Task B pulls value from XCom
Task B uses value
Task A runs and returns a value, which Airflow saves in XCom. Task B then pulls this value from XCom to use it.
Execution Sample
Apache Airflow
def task_a():
    return 'hello from A'

def task_b(ti):
    msg = ti.xcom_pull(task_ids='task_a')
    print(msg)
Task A returns a string; Task B pulls that string from XCom and prints it.
Process Table
StepActionTaskXCom StateOutput/Result
1Run task_a()task_aXCom emptytask_a returns 'hello from A'
2Store return value in XComtask_aXCom stores {'task_a': 'hello from A'}Value saved
3Start task_b()task_bXCom has {'task_a': 'hello from A'}task_b starts
4Pull value from XComtask_bXCom unchangedPulled 'hello from A'
5Print pulled valuetask_bXCom unchangedOutput: hello from A
6task_b endstask_bXCom unchangedExecution complete
💡 task_b finishes after pulling and printing the value from XCom
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
XComemptyempty{'task_a': 'hello from A'}{'task_a': 'hello from A'}{'task_a': 'hello from A'}
msg (in task_b)undefinedundefinedundefined'hello from A''hello from A'
Key Moments - 2 Insights
Why does task_b need to pull the value from XCom instead of receiving it directly?
Because Airflow tasks run independently, the return value from task_a is stored in XCom. task_b must explicitly pull it from XCom, as shown in step 4 of the execution_table.
What happens if task_a does not return anything?
If task_a returns nothing, XCom stores None or no value for task_a. Then, when task_b pulls from XCom (step 4), it gets None, so no useful data is passed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is stored in XCom?
AThe print output from task_b
BAn empty dictionary
CThe string 'hello from A' under task_a key
DNothing is stored yet
💡 Hint
Check the 'XCom State' column at step 2 in execution_table
At which step does task_b retrieve the value from XCom?
AStep 4
BStep 1
CStep 3
DStep 5
💡 Hint
Look for 'Pull value from XCom' action in execution_table
If task_a returned None, what would 'msg' be after step 4?
A'hello from A'
BNone
CAn error
DEmpty string
💡 Hint
Refer to variable_tracker for 'msg' values and key_moments explanation about no return
Concept Snapshot
XCom lets Airflow tasks share data.
Task returns a value, Airflow stores it in XCom.
Other tasks pull this value by task_id.
Use ti.xcom_pull(task_ids='task_id') to get it.
Useful for passing small data between tasks.
Full Transcript
In Airflow, tasks run separately. When a task returns a value, Airflow saves it in a special place called XCom. Another task can then ask XCom for that value using the task's ID. This way, tasks can share information even though they run independently. For example, task_a returns 'hello from A', Airflow stores it, and task_b pulls and prints it. If task_a returns nothing, task_b gets None. This process helps tasks communicate small pieces of data.