-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
305 lines (241 loc) · 10.1 KB
/
pipeline.py
File metadata and controls
305 lines (241 loc) · 10.1 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
# 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.
"""
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.
When the pipeline finishes processing, the original context will be updated
with the final pipeline context data.
Args:
ctx: The pipeline context to use for this pipeline execution.
Returns:
The pipeline instance for method chaining.
"""
# 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.
Args:
t: A callable that takes a transformer and returns a transformed transformer.
Returns:
A new Pipeline with the transformed data.
"""
# 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.
The pipeline's managed context is passed down to the transformer.
Args:
transformer: Either a Transformer instance or a callable function
that processes the data.
Returns:
A new Pipeline with the transformed data.
Raises:
TypeError: If the transformer is not a supported type.
"""
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."""
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.
Returns:
An iterator over the processed data.
"""
yield from self.processed_data
def to_list(self) -> list[T]:
"""Execute the pipeline and return the results as a list.
Returns:
A list containing all processed items from the pipeline.
"""
return list(self.processed_data)
def each(self, function: PipelineFunction[T]) -> None:
"""Apply a function to each element (terminal operation).
Args:
function: The function to apply to each element.
"""
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).
Args:
n: The number of elements to retrieve.
Returns:
A list containing the first n elements.
Raises:
AssertionError: If n is less than 1.
"""
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.
This is useful when you want to execute the pipeline for side effects
without collecting the results.
"""
for _ in self.processed_data:
pass