-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.py
339 lines (273 loc) · 13.7 KB
/
pool.py
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
import concurrent.futures
import multiprocess as mp
import inspect
import logging
from heapq import heappush, heappop
from .constants import ExceptionBehaviour, ArgumentPassing
from .delayed_init import ShellObject
from .utils import is_sized_iterator, is_exception
def is_valid_worker(worker):
return inspect.isfunction(worker) or type(worker) == ShellObject,
class WorkerWrapper:
def __init__(self, worker, timeout):
self.worker = worker
self.timeout = timeout
def __call__(self, in_queue, out_queue, argument_type: ArgumentPassing):
with concurrent.futures.ThreadPoolExecutor() as executor:
while True:
packed_arg = in_queue.get()
if packed_arg is None:
break
obj_id, worker_arg = packed_arg
# If a worker is an object with a delayed initialization (inside a shell object),
# then it will be created the first time it is used here.
try:
if argument_type == ArgumentPassing.AS_KWARGS:
future = executor.submit(self.worker, **worker_arg)
ret_val = future.result(timeout=self.timeout)
elif argument_type == ArgumentPassing.AS_ARGS:
future = executor.submit(self.worker, *worker_arg)
ret_val = future.result(timeout=self.timeout)
elif argument_type == ArgumentPassing.AS_SINGLE_ARG:
future = executor.submit(self.worker, worker_arg)
ret_val = future.result(timeout=self.timeout)
else:
raise Exception(f'Invalid argument passing type: {argument_type}')
except Exception as e:
ret_val = e
out_queue.put((obj_id, ret_val))
#
# This resource clean-up is key. Quite interesting, we pass test_queue_cleanup_after_exception_worker
# which checks termination due to an exception (with 'immediate') in the unbounded model
# Yet on some real tasks, the function __call_ terminates properly, but the process does not finish
# due to queue threads being active.
#
in_queue.cancel_join_thread()
out_queue.cancel_join_thread()
class SortedOutputHelper:
"""
The processed results may come in (somewhat) unordered, but we need to output them using the original order.
This class maintains a priority queue to achieve this. An important assumption: all objects will be
enumerated from 0 to <number of objects - 1> without gaps and repetitions.
"""
def __init__(self):
self.last_obj_out = -1
self.out_queue = []
def add_obj(self, obj_id, obj_ref):
heappush(self.out_queue, (obj_id, obj_ref))
def yield_results(self):
while self.out_queue and self.out_queue[0][0] == self.last_obj_out + 1:
self.last_obj_out, result = heappop(self.out_queue)
yield result
def empty(self):
return not self.out_queue
class WorkerPoolResultGenerator:
def __init__(self, parent_obj, input_iterable,
bounded,
is_unordered,
chunk_size, chunk_prefill_ratio):
self.parent_obj = parent_obj
self.input_iter = iter(input_iterable)
self.is_unordered = is_unordered
self.bounded = bounded
self.chunk_size = chunk_size
self.chunk_prefill_ratio = chunk_prefill_ratio
assert self.chunk_size >= 1
assert self.chunk_prefill_ratio >= 1
# If the length is None, then TQDM will not know the total length and will not display the progress bar:
# See __len__ function https://github.com/tqdm/tqdm/blob/master/tqdm/std.py
if is_sized_iterator(input_iterable):
self._length = len(input_iterable) # Store the length of the iterable
else:
self._length = None
self._iterator = self._generator() # Create the generator
def __len__(self):
return self._length
def __iter__(self):
return self
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.parent_obj.__exit__(exc_type, exc_val, exc_tb)
def __next__(self):
return next(self._iterator)
def _generator(self):
submitted_qty = 0
received_qty = 0
assert type(self.chunk_size) == int
if self.is_unordered:
assert type(self.chunk_prefill_ratio) == int and self.chunk_prefill_ratio >= 1
curr_batch_size = self.chunk_size * self.chunk_prefill_ratio
else:
curr_batch_size = self.chunk_size
finished_input = False
exceptions_arr = []
sorted_out_helper = SortedOutputHelper()
while not finished_input or received_qty < submitted_qty:
try:
curr_submit_qty = 0
while True:
self.parent_obj.in_queue.put((submitted_qty, next(self.input_iter)))
assert self._length is None or submitted_qty < self._length
submitted_qty += 1
curr_submit_qty += 1
if self.bounded and curr_submit_qty >= curr_batch_size:
break
except StopIteration:
finished_input = True
curr_batch_size = self.chunk_size
left_qty = submitted_qty - received_qty
for k in range(min(self.chunk_size, left_qty)):
obj_id, result = self.parent_obj.out_queue.get()
if is_exception(result):
if self.parent_obj.exception_behavior == ExceptionBehaviour.IMMEDIATE:
# Cleaning the input queue (else in unbounded mode we have to wait for all other tasks to finish)
self.clean_input_queue()
self.parent_obj._close()
raise result
elif self.parent_obj.exception_behavior == ExceptionBehaviour.DEFERRED:
exceptions_arr.append(result)
else:
# If exception is ignored it will be returned to the end user
assert self.parent_obj.exception_behavior == ExceptionBehaviour.IGNORE
if self.is_unordered:
yield result
else:
sorted_out_helper.add_obj(obj_id, result)
for result in sorted_out_helper.yield_results():
yield result
assert received_qty < submitted_qty
assert self._length is None or received_qty < self._length
# We update this counter after receiving an element from the queue rather than after
# returning/yielding it. If the priority queue is not empty after all elements are processed
# and received, we will still empty it afer exiting the outer loop.
received_qty += 1
for result in sorted_out_helper.yield_results():
yield result
for result in sorted_out_helper.yield_results():
yield result
assert sorted_out_helper.empty(), \
f'Logic error, the output queue should be empty at this point, but it has {len(out_queue)} elements'
self.parent_obj._close()
if exceptions_arr:
raise Exception(*exceptions_arr)
def clean_input_queue(self):
try:
while not self.parent_obj.in_queue.empty():
# Some small non-zero timeout is fine
self.parent_obj.in_queue.get(1e-6)
except:
pass
class Pool:
"""
A class representing a pool of workers for parallel processing.
This class manages a pool of worker processes or threads that can execute tasks in parallel.
It supports both bounded and unbounded execution modes, stateless and stateful workers. Moreover,
it can handle different argument passing strategies and exception behaviors.
"""
def __enter__(self):
return self
def __call__(self, input_iterable):
"""
Call the Pool object as a function to process the input iterable.
:param input_iterable: An iterable containing inputs to be processed
:return: A generator yielding results from the worker pool. This generator is also a context manager.
:rtype: :class:`WorkerPoolResultGenerator`
"""
assert self.chunk_size >= 1
assert self.chunk_prefill_ratio >= 1
return WorkerPoolResultGenerator(parent_obj=self, input_iterable=input_iterable,
is_unordered=self.is_unordered, bounded=self.bounded,
chunk_size=self.chunk_size,
chunk_prefill_ratio=self.chunk_prefill_ratio)
def __init__(self, worker_or_worker_arr,
n_jobs: int = None,
argument_type: ArgumentPassing = ArgumentPassing.AS_SINGLE_ARG,
exception_behavior: ExceptionBehaviour = ExceptionBehaviour.IMMEDIATE,
bounded: bool = True,
chunk_size: int = None, chunk_prefill_ratio: int = None,
is_unordered: bool = False,
use_threads: bool = False,
task_timeout: float = None,
join_timeout: float = None):
"""
Initialize the Pool object with the given parameters.
:param worker_or_worker_arr: A single worker function/object or a list of worker functions/objects
:param n_jobs: Number of worker processes/threads to create (ignored if worker_or_worker_arr is a list)
:param argument_type: Specifies how arguments are passed to workers
:param exception_behavior: Defines how exceptions are handled
:param bounded: Whether to use bounded execution mode: The bounded execution mode is memory efficient.
In the unbounded execution mode, all input items are loaded into memory.
:param chunk_size: Size of chunk
:param chunk_prefill_ratio: Prefill ratio for chunks
:param is_unordered: Whether results can be returned in any order
:param use_threads: Use threads instead of processes
:param task_timeout: Timeout for individual tasks (currently discouraged)
:param join_timeout: Timeout for joining workers
"""
if task_timeout is not None:
logging.warning("The task timeout features is deprecated."
" We currently cannot support task timeouts in the safe and cross-platform fashion")
if type(worker_or_worker_arr) == list:
assert n_jobs is None or n_jobs == len(worker_or_worker_arr), \
'The number of workers does not match the worker array length (you can just set it None)!'
self.num_workers = len(worker_or_worker_arr)
for worker in worker_or_worker_arr:
assert is_valid_worker(worker), \
f'A worker must be a function or an instance of a class with a delayed initialization, ' + \
' not {type(worker)}!'
else:
assert n_jobs is not None, 'Specify the number of jobs or an array of worker objects!'
assert is_valid_worker(worker_or_worker_arr), \
f'A worker must be a function or an instance of a class with a delayed initialization,' + \
' not {type(function_or_worker_arr)}!'
self.num_workers = max(int(n_jobs), 1)
self.bounded = bounded
self.chunk_prefill_ratio = max(int(chunk_prefill_ratio), 1) if chunk_prefill_ratio is not None else 2
self.chunk_size = max(int(chunk_size), 1) if chunk_size is not None else self.num_workers
self.exception_behavior = exception_behavior
self.argument_type = argument_type
self.is_unordered = is_unordered
self.in_queue = mp.Queue()
self.out_queue = mp.Queue()
self.term_signal_sent = False
self.exited = False
self.use_threads = use_threads
if self.use_threads:
import threading
process_class = threading.Thread
daemon = None
else:
process_class = mp.Process
daemon = True
self.join_timeout = join_timeout
self.task_timeout = task_timeout
self.workers = []
# Start worker processes
for proc_id in range(self.num_workers):
one_worker = worker_or_worker_arr[proc_id] \
if type(worker_or_worker_arr) == list else worker_or_worker_arr
one_proc = process_class(target=WorkerWrapper(one_worker, self.task_timeout),
args=(self.in_queue, self.out_queue, self.argument_type),
daemon=daemon)
self.workers.append(one_proc)
one_proc.start()
def __exit__(self, type, value, tb):
# Close will not do anything if the close function was called already
self._close()
# If a process "refuses" to stop it can be terminated.
# Unfortunately, threads cannot be stopped / terminated in Python,
# but they will die when the main process terminates.
if not self.use_threads:
for p in self.workers:
p.terminate()
self.exited = True
def _join_workers(self):
for p in self.workers:
p.join(self.join_timeout)
def _close(self):
if not self.term_signal_sent:
for _ in range(self.num_workers):
self.in_queue.put(None) # end-of-work signal: one per worker
self._join_workers()
self.term_signal_sent = True