You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2025/06/10 11:02:33 UTC
[PR] Improve `xcom_pull` to reflect reality for mapped tasks [airflow]
amoghrajesh opened a new pull request, #51568:
URL: https://github.com/apache/airflow/pull/51568
<!--
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.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139524533
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -338,19 +338,34 @@ def xcom_pull(
run_id = self.run_id
single_task_requested = isinstance(task_ids, (str, type(None)))
- single_map_index_requested = isinstance(map_indexes, (int, type(None), ArgNotSet))
+ single_map_index_requested = isinstance(map_indexes, (int, type(None)))
if task_ids is None:
# default to the current task if not provided
task_ids = [self.task_id]
elif isinstance(task_ids, str):
task_ids = [task_ids]
- map_indexes_iterable: Iterable[int | None] = []
- # If map_indexes is not provided, default to use the map_index of the calling task
+ # If map_indexes is not specified, pull xcoms from all map indexes for each task
if isinstance(map_indexes, ArgNotSet):
- map_indexes_iterable = [self.map_index]
- elif isinstance(map_indexes, int) or map_indexes is None:
+ xcoms = []
+ for t_id in task_ids:
+ values = XCom.get_all(
+ run_id=run_id,
+ key=key,
+ task_id=t_id,
+ dag_id=dag_id,
+ )
+ xcoms.extend(values)
Review Comment:
```suggestion
xcoms = [
values
for t_id in task_ids
for value in XCom.get_all(
run_id=run_id,
key=key,
task_id=t_id,
dag_id=dag_id,
)
]
```
Not sure if this would be readable enough. It should be slightly more performant.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #51568:
URL: https://github.com/apache/airflow/pull/51568#issuecomment-2964975856
Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139753381
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -338,19 +338,34 @@ def xcom_pull(
run_id = self.run_id
single_task_requested = isinstance(task_ids, (str, type(None)))
- single_map_index_requested = isinstance(map_indexes, (int, type(None), ArgNotSet))
+ single_map_index_requested = isinstance(map_indexes, (int, type(None)))
if task_ids is None:
# default to the current task if not provided
task_ids = [self.task_id]
elif isinstance(task_ids, str):
task_ids = [task_ids]
- map_indexes_iterable: Iterable[int | None] = []
- # If map_indexes is not provided, default to use the map_index of the calling task
+ # If map_indexes is not specified, pull xcoms from all map indexes for each task
if isinstance(map_indexes, ArgNotSet):
- map_indexes_iterable = [self.map_index]
- elif isinstance(map_indexes, int) or map_indexes is None:
+ xcoms = []
+ for t_id in task_ids:
+ values = XCom.get_all(
+ run_id=run_id,
+ key=key,
+ task_id=t_id,
+ dag_id=dag_id,
+ )
+ xcoms.extend(values)
Review Comment:
Yea sure, if its performant, we can do it.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #51568:
URL: https://github.com/apache/airflow/pull/51568#issuecomment-2964974537
Thanks for the review, merging it.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh merged PR #51568:
URL: https://github.com/apache/airflow/pull/51568
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139494634
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
+
+ If there are no results, *None* is returned. If XCom entries exist,
+ a list containing all matching XCom values is returned.
+
+ This is particularly useful for getting all XCom values from all map
+ indexes of a mapped task at once.
+
+ :param key: A key for the XCom. Only XComs with this key will be returned.
+ :param run_id: DAG run ID for the task.
+ :param dag_id: DAG ID to pull XComs from.
+ :param task_id: Task ID to pull XComs from.
+ :return: List of all XCom values if found, None if no XComs exist.
+ """
+ from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+
+ # Since Triggers can hit this code path via `sync_to_async` (which uses threads internally)
+ # we need to make sure that we "atomically" send a request and get the response to that
+ # back so that two triggers don't end up interleaving requests and create a possible
+ # race condition where the wrong trigger reads the response.
+ with SUPERVISOR_COMMS.lock:
+ SUPERVISOR_COMMS.send_request(
+ log=log,
+ msg=GetXComSequenceSlice(
+ key=key,
+ dag_id=dag_id,
+ task_id=task_id,
+ run_id=run_id,
+ start=None,
+ stop=None,
+ step=None,
+ ),
+ )
+ msg = SUPERVISOR_COMMS.get_message()
+
+ if not isinstance(msg, XComSequenceSliceResult):
+ raise TypeError(f"Expected XComSequenceSliceResult, received: {type(msg)} {msg}")
+
+ if msg.root is not None:
+ return msg.root
+ return None
Review Comment:
Makes sense, let me do that
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139468805
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
Review Comment:
But this doesn’t? It never calls `deserialize_value`.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on PR #51568:
URL: https://github.com/apache/airflow/pull/51568#issuecomment-3023282448
@sdg9670f Historical compat. Most of the time you shouldn't be using methods on `XCom` directly anyway
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139505048
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
Review Comment:
Handled in [review comments from TP](https://github.com/apache/airflow/pull/51568/commits/a2c01b1176f41e29dbd5238e93b14a231216441a)
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
+
+ If there are no results, *None* is returned. If XCom entries exist,
+ a list containing all matching XCom values is returned.
+
+ This is particularly useful for getting all XCom values from all map
+ indexes of a mapped task at once.
+
+ :param key: A key for the XCom. Only XComs with this key will be returned.
+ :param run_id: DAG run ID for the task.
+ :param dag_id: DAG ID to pull XComs from.
+ :param task_id: Task ID to pull XComs from.
+ :return: List of all XCom values if found, None if no XComs exist.
+ """
+ from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+
+ # Since Triggers can hit this code path via `sync_to_async` (which uses threads internally)
+ # we need to make sure that we "atomically" send a request and get the response to that
+ # back so that two triggers don't end up interleaving requests and create a possible
+ # race condition where the wrong trigger reads the response.
+ with SUPERVISOR_COMMS.lock:
+ SUPERVISOR_COMMS.send_request(
+ log=log,
+ msg=GetXComSequenceSlice(
+ key=key,
+ dag_id=dag_id,
+ task_id=task_id,
+ run_id=run_id,
+ start=None,
+ stop=None,
+ step=None,
+ ),
+ )
+ msg = SUPERVISOR_COMMS.get_message()
+
+ if not isinstance(msg, XComSequenceSliceResult):
+ raise TypeError(f"Expected XComSequenceSliceResult, received: {type(msg)} {msg}")
+
+ if msg.root is not None:
+ return msg.root
+ return None
Review Comment:
Handled in [review comments from TP](https://github.com/apache/airflow/pull/51568/commits/a2c01b1176f41e29dbd5238e93b14a231216441a)
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139524533
##########
task-sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -338,19 +338,34 @@ def xcom_pull(
run_id = self.run_id
single_task_requested = isinstance(task_ids, (str, type(None)))
- single_map_index_requested = isinstance(map_indexes, (int, type(None), ArgNotSet))
+ single_map_index_requested = isinstance(map_indexes, (int, type(None)))
if task_ids is None:
# default to the current task if not provided
task_ids = [self.task_id]
elif isinstance(task_ids, str):
task_ids = [task_ids]
- map_indexes_iterable: Iterable[int | None] = []
- # If map_indexes is not provided, default to use the map_index of the calling task
+ # If map_indexes is not specified, pull xcoms from all map indexes for each task
if isinstance(map_indexes, ArgNotSet):
- map_indexes_iterable = [self.map_index]
- elif isinstance(map_indexes, int) or map_indexes is None:
+ xcoms = []
+ for t_id in task_ids:
+ values = XCom.get_all(
+ run_id=run_id,
+ key=key,
+ task_id=t_id,
+ dag_id=dag_id,
+ )
+ xcoms.extend(values)
Review Comment:
```suggestion
xcoms = [
value
for t_id in task_ids
for value in XCom.get_all(
run_id=run_id,
key=key,
task_id=t_id,
dag_id=dag_id,
)
]
```
Not sure if this would be readable enough. It should be slightly more performant.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139467069
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
+
+ If there are no results, *None* is returned. If XCom entries exist,
+ a list containing all matching XCom values is returned.
+
+ This is particularly useful for getting all XCom values from all map
+ indexes of a mapped task at once.
+
+ :param key: A key for the XCom. Only XComs with this key will be returned.
+ :param run_id: DAG run ID for the task.
+ :param dag_id: DAG ID to pull XComs from.
+ :param task_id: Task ID to pull XComs from.
+ :return: List of all XCom values if found, None if no XComs exist.
+ """
+ from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+
+ # Since Triggers can hit this code path via `sync_to_async` (which uses threads internally)
+ # we need to make sure that we "atomically" send a request and get the response to that
+ # back so that two triggers don't end up interleaving requests and create a possible
+ # race condition where the wrong trigger reads the response.
+ with SUPERVISOR_COMMS.lock:
+ SUPERVISOR_COMMS.send_request(
+ log=log,
+ msg=GetXComSequenceSlice(
+ key=key,
+ dag_id=dag_id,
+ task_id=task_id,
+ run_id=run_id,
+ start=None,
+ stop=None,
+ step=None,
+ ),
+ )
+ msg = SUPERVISOR_COMMS.get_message()
+
+ if not isinstance(msg, XComSequenceSliceResult):
+ raise TypeError(f"Expected XComSequenceSliceResult, received: {type(msg)} {msg}")
+
+ if msg.root is not None:
+ return msg.root
+ return None
Review Comment:
XComSequenceSliceResult should never contain None, `root` would always be a list. It returns an empty list if there are no values (same behaviour as slicing a Python list). This should probably simply `return msg.root` unconditionally.
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve `xcom_pull` to cover different scenarios for mapped tasks [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #51568:
URL: https://github.com/apache/airflow/pull/51568#discussion_r2139498887
##########
task-sdk/src/airflow/sdk/bases/xcom.py:
##########
@@ -274,6 +281,61 @@ def get_one(
)
return None
+ @classmethod
+ def get_all(
+ cls,
+ *,
+ key: str,
+ dag_id: str,
+ task_id: str,
+ run_id: str,
+ ) -> Any | None:
+ """
+ Retrieve all XCom values for a task, typically from all map indexes.
+
+ This method returns "full" XCom values (i.e. uses ``deserialize_value``
+ from the XCom backend).
Review Comment:
Copy paste error :D
removing it
--
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: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org