Process items one at a time with minimal memory usage
| 133 | yield items[i:i + self.batch_size] |
| 134 | |
| 135 | class 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 |
| 189 | def batch_chunks_by_document(chunks: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]: |
nothing calls this directly
no outgoing calls
no test coverage detected