Xcom In Airflow ❲LEGIT | REVIEW❳
@task def extract() -> dict: return "user_id": 123, "name": "Alice" # pushed automatically
aggregate(download.expand(url=fetch_urls()))
XComs are for coordination , not data transfer . Final Takeaway XComs are Airflow’s glue. They turn a set of isolated tasks into a coherent pipeline. Use them for small control signals, IDs, and results. Keep them light. And when you’re tempted to pass a big blob of data – stop, and ask yourself: should this be in object storage instead? xcom in airflow
from airflow.operators.python import PythonOperator def push_function(**context): context['ti'].xcom_push(key='user_id', value=123)
process(extract()) # XCom passed implicitly @task def extract() -> dict: return "user_id": 123,
from airflow.decorators import dag, task from datetime import datetime @dag(start_date=datetime(2024,1,1), schedule=None, catchup=False) def xcom_demo():
push = PythonOperator(task_id='push_task', python_callable=push_function) pull = PythonOperator(task_id='pull_task', python_callable=pull_function) @task def extract() ->
@task def process(user_data: dict) -> str: return f"Processed user user_data['name']"