Airflow: how to use xcom_push and xcom_pull in non-PythonOperator

You can access XCom variables from within templated fields. For example, to read from XCom:

myOperator = MyOperator(
    message="Operation result: {{ task_instance.xcom_pull(task_ids=['task1', 'task2'], key='result_status') }}",
    ...

It is also possible to not specify task to get all XCom pushes within one DagRun with the same key name

myOperator = MyOperator(
    message="Warning status: {{ task_instance.xcom_pull(task_ids=None, key='warning_status') }}",
    ...

would return an array.


From inside an operator's execute method:

Push:

self.xcom_push(context, key, value)

Pull:

self.xcom_pull(context, key=key)

If you have a task instance:

Push:

context["ti"].xcom_push(key, value)

Pull:

context["ti"].xcom_pull(key=key)

Tags:

Airflow