MCPcopy Create free account
hub / github.com/PromtEngineer/localGPT / StreamingProcessor

Class StreamingProcessor

rag_system/utils/batch_processor.py:135–186  ·  view source on GitHub ↗

Process items one at a time with minimal memory usage

Source from the content-addressed store, hash-verified

133 yield items[i:i + self.batch_size]
134
135class StreamingProcessor:
136 """Process items one at a time with minimal memory usage"""
137
138 def __init__(self, enable_gc_interval: int = 100):
139 self.enable_gc_interval = enable_gc_interval
140
141 def process_streaming(
142 self,
143 items: List[Any],
144 process_func: Callable,
145 operation_name: str = "Streaming Processing",
146 **kwargs
147 ) -> List[Any]:
148 """
149 Process items one at a time with minimal memory footprint
150
151 Args:
152 items: List of items to process
153 process_func: Function to process each item
154 operation_name: Name for progress reporting
155 **kwargs: Additional arguments passed to process_func
156
157 Returns:
158 List of results
159 """
160 if not items:
161 logger.info(f"{operation_name}: No items to process")
162 return []
163
164 tracker = ProgressTracker(len(items), operation_name)
165 results = []
166
167 logger.info(f"Starting {operation_name} for {len(items)} items (streaming)")
168
169 with timer(f"{operation_name} (streaming)"):
170 for i, item in enumerate(items):
171 try:
172 result = process_func(item, **kwargs)
173 results.append(result)
174 tracker.update(1)
175
176 except Exception as e:
177 logger.error(f"Error processing item {i}: {e}")
178 tracker.update(1, errors=1)
179 continue
180
181 # Periodic garbage collection
182 if self.enable_gc_interval and (i + 1) % self.enable_gc_interval == 0:
183 gc.collect()
184
185 tracker.finish()
186 return results
187
188# Utility functions for common batch operations
189def batch_chunks_by_document(chunks: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected