## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Example DAG demonstrating the usage of XComs."""from__future__importannotationsimportpendulumfromairflow.decoratorsimporttaskfromairflow.models.dagimportDAGfromairflow.models.xcom_argimportXComArgfromairflow.operators.bashimportBashOperator
[docs]defpush(ti=None):"""Pushes an XCom without a specific target"""ti.xcom_push(key="value from pusher 1",value=value_1)
@task
[docs]defpush_by_returning():"""Pushes an XCom without a specific target, just by returning it"""returnvalue_2
def_compare_values(pulled_value,check_value):ifpulled_value!=check_value:raiseValueError(f"The two values differ {pulled_value} and {check_value}")@task
[docs]defpuller(pulled_value_2,ti=None):"""Pull all previously pushed XComs and check if the pushed values match the pulled values."""pulled_value_1=ti.xcom_pull(task_ids="push",key="value from pusher 1")_compare_values(pulled_value_1,value_1)_compare_values(pulled_value_2,value_2)
@task
[docs]defpull_value_from_bash_push(ti=None):bash_pushed_via_return_value=ti.xcom_pull(key="return_value",task_ids="bash_push")bash_manually_pushed_value=ti.xcom_pull(key="manually_pushed_value",task_ids="bash_push")print(f"The xcom value pushed by task push via return value is {bash_pushed_via_return_value}")print(f"The xcom value pushed by task push manually is {bash_manually_pushed_value}")