10

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!

0

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't address pushing from a non-PythonOperator.
Correct. In my version of the question, only "I need to do xcom_pull from a non-PythonOperator" was asked.
4

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)

2 Comments

Would you please elaborate this answer by mentioning, where these lines can be added?
Ok, added more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.