Airflow Xcom Example

def auto_push(): return "auto_xcom" # automatically in XCom

There are three main ways to interact with XComs:

1/4 Ever needed to pass a value from one Airflow task to another? → Use (Cross-Communication). Think of it as a mini shared dictionary for your DAG run. 🧠 airflow xcom example

from airflow.decorators import dag, task from pendulum import datetime @dag(start_date=datetime(2023, 1, 1), schedule=None, catchup=False) def xcom_taskflow_dag(): @task def generate_value(): # This return value is automatically pushed to XCom return "order_id": 1234, "status": "processed" @task def process_value(order_data): # Airflow automatically pulls the XCom data here print(f"Processing order: order_data['order_id']") # Setting the dependency naturally passes the data order_info = generate_value() process_value(order_info) xcom_taskflow_dag() Use code with caution. The Traditional Way: Explicit Push/Pull

from airflow.operators.python import PythonOperator def auto_push(): return "auto_xcom" # automatically in XCom

One of the first hurdles beginners face when learning Apache Airflow is data sharing. You write a task that processes a file, and you want to pass the result to the next task. But how?

In this post, we will break down what XComs are, how to use them, and provide a concrete code example. 🧠 from airflow

user = context['ti'].xcom_pull(task_ids='task_a', key='user_id')