kee
kee

Reputation: 11629

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

I see a lot of examples on how to use xcom_push and xcom_pull with PythonOperators in Airflow.

I need to do xcom_pull from a non-PythonOperator class and couldn't find how to do it.

Any pointer or example will be appreciated!

Upvotes: 10

Views: 20108

Answers (2)

bcb
bcb

Reputation: 2078

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)

Upvotes: 4

tobi6
tobi6

Reputation: 8239

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.

Upvotes: 8

Related Questions