uranusjr commented on code in PR #22849:
URL: https://github.com/apache/airflow/pull/22849#discussion_r846333236


##########
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())
+
+    def __getitem__(self, key):
+        if not isinstance(key, int):
+            raise ValueError("only support index access for now")
+        try:
+            with self._get_bound_query() as query:
+                r = query.offset(key).limit(1).one()
+        except NoResultFound:
+            raise IndexError(key) from None
+        return XCom.deserialize_value(r)

Review Comment:
   Just realised I forgot to handle negative indexes… maybe deal with that when 
someone complains.



-- 
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