dabla commented on PR #57975:
URL: https://github.com/apache/airflow/pull/57975#issuecomment-3501091480

   This is what I came up with:
   
   ```
   class BufferedXComIterator(Iterator[T]):
       def __init__(self, iterator: Iterator, prefetch_size: int | None = None) 
-> None:
           self._delegate = iterator
           self._buffer = deque(maxlen=prefetch_size or cpu_count())
   
       @property
       def prefetch_size(self) -> int:
           return self._buffer.maxlen
   
       def __next__(self) -> T:
           if isinstance(self._delegate, LazyXComIterator):
               if self._delegate.index < 0:
                   # When iterating backwards, avoid extra HTTP request
                   raise StopIteration()
   
               # If the buffer is empty, fetch the next chunk
               if not self._buffer:
                   chunk = list(self._delegate.seq[self._delegate.index: 
self._delegate.index + self.prefetch_size])
                   if not chunk:
                       raise StopIteration()
                   self._buffer.extend(chunk)
   
               val = self._buffer.popleft()
               self._delegate.index += self._delegate.dir
               return val
           return next(self._delegate)
   ```


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