-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
410 lines (327 loc) · 15 KB
/
pipeline.py
File metadata and controls
410 lines (327 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# pipeline.py
from collections.abc import Callable
from collections.abc import Iterable
from collections.abc import Iterator
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import as_completed
import itertools
import multiprocessing as mp
from queue import Queue
from typing import Any
from typing import TypeVar
from typing import overload
from laygo.helpers import PipelineContext
from laygo.helpers import is_context_aware
from laygo.transformers.transformer import Transformer
from laygo.transformers.transformer import passthrough_chunks
T = TypeVar("T")
U = TypeVar("U")
PipelineFunction = Callable[[T], Any]
class Pipeline[T]:
"""Manages a data source and applies transformers to it.
A Pipeline provides a high-level interface for data processing by chaining
transformers together. It automatically manages a multiprocessing-safe
shared context that can be accessed by all transformers in the chain.
The Pipeline supports both streaming and batch processing patterns, with
built-in support for buffering, branching (fan-out), and parallel processing.
Example:
>>> data = [1, 2, 3, 4, 5]
>>> result = (Pipeline(data)
... .transform(lambda t: t.filter(lambda x: x % 2 == 0))
... .transform(lambda t: t.map(lambda x: x * 2))
... .to_list())
>>> result # [4, 8]
Note:
Most pipeline operations consume the internal iterator, making the
pipeline effectively single-use unless the data source is re-initialized.
"""
def __init__(self, *data: Iterable[T]) -> None:
"""Initialize a pipeline with one or more data sources.
Args:
*data: One or more iterable data sources. If multiple sources are
provided, they will be chained together.
Raises:
ValueError: If no data sources are provided.
"""
if len(data) == 0:
raise ValueError("At least one data source must be provided to Pipeline.")
self.data_source: Iterable[T] = itertools.chain.from_iterable(data) if len(data) > 1 else data[0]
self.processed_data: Iterator = iter(self.data_source)
# Always create a shared context with multiprocessing manager
self._manager = mp.Manager()
self.ctx = self._manager.dict()
# Add a shared lock to the context for safe concurrent updates
self.ctx["lock"] = self._manager.Lock()
# Store reference to original context for final synchronization
self._original_context_ref: PipelineContext | None = None
def __del__(self) -> None:
"""Clean up the multiprocessing manager when the pipeline is destroyed."""
try:
self._sync_context_back()
self._manager.shutdown()
except Exception:
pass
def context(self, ctx: PipelineContext) -> "Pipeline[T]":
"""Update the pipeline context and store a reference to the original context.
The provided context will be used during pipeline execution and any
modifications made by transformers will be synchronized back to the
original context when the pipeline finishes processing.
Args:
ctx: The pipeline context dictionary to use for this pipeline execution.
This should be a mutable dictionary-like object that transformers
can use to share state and communicate.
Returns:
The pipeline instance for method chaining.
Note:
Changes made to the context during pipeline execution will be
automatically synchronized back to the original context object
when the pipeline is destroyed or processing completes.
"""
# Store reference to the original context
self._original_context_ref = ctx
# Copy the context data to the pipeline's shared context
self.ctx.update(ctx)
return self
def _sync_context_back(self) -> None:
"""Synchronize the final pipeline context back to the original context reference.
This is called after processing is complete to update the original
context with any changes made during pipeline execution.
"""
if self._original_context_ref is not None:
# Copy the final context state back to the original context reference
final_context_state = dict(self.ctx)
final_context_state.pop("lock", None) # Remove non-serializable lock
self._original_context_ref.clear()
self._original_context_ref.update(final_context_state)
def transform[U](self, t: Callable[[Transformer[T, T]], Transformer[T, U]]) -> "Pipeline[U]":
"""Apply a transformation using a lambda function.
Creates a Transformer under the hood and applies it to the pipeline.
This is a shorthand method for simple transformations that allows
chaining transformer operations in a functional style.
Args:
t: A callable that takes a transformer and returns a transformed transformer.
Typically used with lambda expressions like:
`lambda t: t.map(func).filter(predicate)`
Returns:
A new Pipeline with the transformed data type.
Example:
>>> pipeline = Pipeline([1, 2, 3, 4, 5])
>>> result = pipeline.transform(lambda t: t.filter(lambda x: x % 2 == 0).map(lambda x: x * 2))
>>> result.to_list() # [4, 8]
"""
# Create a new transformer and apply the transformation function
transformer = t(Transformer[T, T]())
return self.apply(transformer)
@overload
def apply[U](self, transformer: Transformer[T, U]) -> "Pipeline[U]": ...
@overload
def apply[U](self, transformer: Callable[[Iterable[T]], Iterator[U]]) -> "Pipeline[U]": ...
@overload
def apply[U](self, transformer: Callable[[Iterable[T], PipelineContext], Iterator[U]]) -> "Pipeline[U]": ...
def apply[U](
self,
transformer: Transformer[T, U]
| Callable[[Iterable[T]], Iterator[U]]
| Callable[[Iterable[T], PipelineContext], Iterator[U]],
) -> "Pipeline[U]":
"""Apply a transformer to the current data source.
This method accepts various types of transformers and applies them to
the pipeline data. The pipeline's managed context is automatically
passed to context-aware transformers.
Args:
transformer: One of the following:
- A Transformer instance (preferred for complex operations)
- A callable function that takes an iterable and returns an iterator
- A context-aware callable that takes an iterable and context
Returns:
The same Pipeline instance with transformed data (for method chaining).
Raises:
TypeError: If the transformer is not a supported type.
Example:
>>> pipeline = Pipeline([1, 2, 3])
>>> # Using a Transformer instance
>>> pipeline.apply(createTransformer(int).map(lambda x: x * 2))
>>> # Using a simple function
>>> pipeline.apply(lambda data: (x * 2 for x in data))
"""
match transformer:
case Transformer():
self.processed_data = transformer(self.processed_data, self.ctx) # type: ignore
case _ if callable(transformer):
if is_context_aware(transformer):
self.processed_data = transformer(self.processed_data, self.ctx) # type: ignore
else:
self.processed_data = transformer(self.processed_data) # type: ignore
case _:
raise TypeError("Transformer must be a Transformer instance or a callable function")
return self # type: ignore
def branch(
self,
branches: dict[str, Transformer[T, Any]],
batch_size: int = 1000,
max_batch_buffer: int = 1,
use_queue_chunks: bool = True,
) -> dict[str, list[Any]]:
"""Forks the pipeline into multiple branches for concurrent, parallel processing.
This is a **terminal operation** that implements a fan-out pattern where
the entire dataset is copied to each branch for independent processing.
Each branch processes the complete dataset concurrently using separate
transformers, and results are collected and returned in a dictionary.
Args:
branches: A dictionary where keys are branch names (str) and values
are `Transformer` instances of any subtype.
batch_size: The number of items to batch together when sending data
to branches. Larger batches can improve throughput but
use more memory. Defaults to 1000.
max_batch_buffer: The maximum number of batches to buffer for each
branch queue. Controls memory usage and creates
backpressure. Defaults to 1.
use_queue_chunks: Whether to use passthrough chunking for the
transformers. When True, batches are processed
as chunks. Defaults to True.
Returns:
A dictionary where keys are the branch names and values are lists
of all items processed by that branch's transformer.
Note:
This operation consumes the pipeline's iterator, making subsequent
operations on the same pipeline return empty results.
"""
if not branches:
self.consume()
return {}
source_iterator = self.processed_data
branch_items = list(branches.items())
num_branches = len(branch_items)
final_results: dict[str, list[Any]] = {}
queues = [Queue(maxsize=max_batch_buffer) for _ in range(num_branches)]
def producer() -> None:
"""Reads from the source and distributes batches to ALL branch queues."""
# Use itertools.batched for clean and efficient batch creation.
for batch_tuple in itertools.batched(source_iterator, batch_size):
# The batch is a tuple; convert to a list for consumers.
batch_list = list(batch_tuple)
for q in queues:
q.put(batch_list)
# Signal to all consumers that the stream is finished.
for q in queues:
q.put(None)
def consumer(transformer: Transformer, queue: Queue) -> list[Any]:
"""Consumes batches from a queue and runs them through a transformer."""
def stream_from_queue() -> Iterator[T]:
while (batch := queue.get()) is not None:
yield batch
if use_queue_chunks:
transformer = transformer.set_chunker(passthrough_chunks)
result_iterator = transformer(stream_from_queue(), self.ctx) # type: ignore
return list(result_iterator)
with ThreadPoolExecutor(max_workers=num_branches + 1) as executor:
executor.submit(producer)
future_to_name = {
executor.submit(consumer, transformer, queues[i]): name for i, (name, transformer) in enumerate(branch_items)
}
for future in as_completed(future_to_name):
name = future_to_name[future]
try:
final_results[name] = future.result()
except Exception as e:
print(f"Branch '{name}' raised an exception: {e}")
final_results[name] = []
return final_results
def buffer(self, size: int, batch_size: int = 1000) -> "Pipeline[T]":
"""Inserts a buffer in the pipeline to allow downstream processing to read ahead.
This creates a background thread that reads from the upstream data source
and fills a queue, decoupling the upstream and downstream stages.
Args:
size: The number of **batches** to hold in the buffer.
batch_size: The number of items to accumulate per batch.
Returns:
The pipeline instance for method chaining.
"""
source_iterator = self.processed_data
def _buffered_stream() -> Iterator[T]:
queue = Queue(maxsize=size)
# We only need one background thread for the producer.
executor = ThreadPoolExecutor(max_workers=1)
def _producer() -> None:
"""The producer reads from the source and fills the queue."""
try:
for batch_tuple in itertools.batched(source_iterator, batch_size):
queue.put(list(batch_tuple))
finally:
# Always put the sentinel value to signal the end of the stream.
queue.put(None)
# Start the producer in the background thread.
executor.submit(_producer)
try:
# The main thread becomes the consumer.
while (batch := queue.get()) is not None:
yield from batch
finally:
# Ensure the background thread is cleaned up.
executor.shutdown(wait=False, cancel_futures=True)
self.processed_data = _buffered_stream()
return self
def __iter__(self) -> Iterator[T]:
"""Allow the pipeline to be iterated over.
This makes the Pipeline compatible with Python's iterator protocol,
allowing it to be used in for loops, list comprehensions, and other
contexts that expect an iterable.
Returns:
An iterator over the processed data.
Note:
This operation consumes the pipeline's iterator, making subsequent
operations on the same pipeline return empty results.
"""
yield from self.processed_data
def to_list(self) -> list[T]:
"""Execute the pipeline and return the results as a list.
This is a terminal operation that consumes the pipeline's iterator
and materializes all results into memory.
Returns:
A list containing all processed items from the pipeline.
Note:
This operation consumes the pipeline's iterator, making subsequent
operations on the same pipeline return empty results.
"""
return list(self.processed_data)
def each(self, function: PipelineFunction[T]) -> None:
"""Apply a function to each element (terminal operation).
This is a terminal operation that processes each element for side effects
and consumes the pipeline's iterator without returning results.
Args:
function: The function to apply to each element. Should be used for
side effects like logging, updating external state, etc.
Note:
This operation consumes the pipeline's iterator, making subsequent
operations on the same pipeline return empty results.
"""
for item in self.processed_data:
function(item)
def first(self, n: int = 1) -> list[T]:
"""Get the first n elements of the pipeline (terminal operation).
This is a terminal operation that consumes up to n elements from the
pipeline's iterator and returns them as a list.
Args:
n: The number of elements to retrieve. Must be at least 1.
Returns:
A list containing the first n elements, or fewer if the pipeline
contains fewer than n elements.
Raises:
AssertionError: If n is less than 1.
Note:
This operation partially consumes the pipeline's iterator. Subsequent
operations will continue from where this operation left off.
"""
assert n >= 1, "n must be at least 1"
return list(itertools.islice(self.processed_data, n))
def consume(self) -> None:
"""Consume the pipeline without returning results (terminal operation).
This is a terminal operation that processes all elements in the pipeline
for their side effects without materializing any results. Useful when
the pipeline operations have side effects and you don't need the results.
Note:
This operation consumes the pipeline's iterator, making subsequent
operations on the same pipeline return empty results.
"""
for _ in self.processed_data:
pass