GlenboLake commented on issue #51821:
URL: https://github.com/apache/airflow/issues/51821#issuecomment-4138809132

   @foldvari My temporary solution was to implement this helper function:
   ```python
   def xcom_pull_one(ti: RuntimeTaskInstanceProtocol, dag: DAG, key: str, 
**kwargs):
       """
       Wrapper around ti.xcom_pull that considers all task_ids
   
       Checks XComs for all tasks in the DAG and returns the first non-None
       value encountered.
   
       CAUTION: This may not be deterministic if multiple tasks
       have set XComs with the same key.
       """
       for task_id in dag.task_ids:
           if (value := ti.xcom_pull(key=key, task_ids=task_id, **kwargs)) is 
not None:
               return value
       return None
   ```
   I can't say I would actually recommend it, as indicated by the docstring; it 
seems as though the Airflow 2 behavior was never intended. I'm currently in the 
process of updating all my DAGs so this helper function is no longer necessary 
and I don't intend for it to ever hit my production environment.
   
   I have several cases that do something like in Airflow 2:
   ```python
   def push(ti):
       ti.xcom_push(key='foo', value=5)
   
   push_xcom = PythonOperator(
       task_id='push_xcom',
       python_callable=push,
   )
   
   def pull(ti, xcom_key):
       some_value = ti.xcom_pull(key=xcom_key)
   
   pull_xcom = PythonOperator(
       task_id='pull_xcom',
       python_callable=pull,
       op_kwargs={'xcom_key': 'foo'}
   )
   push_xcom >> pull_xcom
   ```
   And I'm working on updating them to work like this in Airflow 3:
   ```python
   def pull(ti, xcom):
       some_value = ti.xcom_pull(**xcom)
   
   pull_xcom = PythonOperator(
       task_id='pull_xcom',
       python_callable=pull,
       op_kwargs={
           'xcom': {'key': 'foo', 'task_ids': push_xcom.task_id}
   )
   ```
   If none of that works for you, then there's always [this REST 
endpoint](https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#operation/get_xcom_entries).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to