ashb commented on code in PR #22849:
URL: https://github.com/apache/airflow/pull/22849#discussion_r846319944
##########
airflow/models/taskinstance.py:
##########
@@ -295,6 +299,58 @@ def clear_task_instances(
dr.start_date = None
+class _LazyXComAccessIterator(collections.abc.Iterator):
+ def __init__(self, cm: ContextManager[Query]):
+ self._cm = cm
+ self._it = iter(cm.__enter__())
+
+ def __del__(self):
+ self._cm.__exit__()
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ return XCom.deserialize_value(next(self._it))
+
+
+class _LazyXComAccess(collections.abc.Sequence):
+ """Wrapper to lazily pull XCom with a sequence-like interface.
+
+ Note that since the session bound to the parent query may have died when we
+ actually access the sequence's content, we must create a new fresh session
+ for every function call with ``with_session()``.
+ """
+
+ def __init__(self, query: Query):
+ self._q = query
+
+ def __len__(self):
+ with self._get_bound_query() as query:
+ return query.count()
+
+ def __iter__(self):
+ return _LazyXComAccessIterator(self._get_bound_query())
Review Comment:
Oh nice, this means we can iterate over the list multiple times in a
consuming task. Makes the docs easier to write.
--
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]