forked from frnsys/bz_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.py
31 lines (29 loc) · 1011 Bytes
/
parallel.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
from tqdm import tqdm
from concurrent.futures import ProcessPoolExecutor, as_completed
def run_parallel(arr, fn, n_jobs=8, use_kwargs=False):
if n_jobs==1:
return [fn(**a) if use_kwargs else fn(a) for a in tqdm(arr)]
#Assemble the workers
with ProcessPoolExecutor(max_workers=n_jobs) as pool:
#Pass the elements of array into function
if use_kwargs:
futures = [pool.submit(fn, **a) for a in arr]
else:
futures = [pool.submit(fn, a) for a in arr]
kwargs = {
'total': len(futures),
'unit': 'it',
'unit_scale': True,
'leave': True
}
#Print out the progress as tasks complete
for f in tqdm(as_completed(futures), **kwargs):
pass
out = []
#Get the results from the futures.
for i, future in tqdm(enumerate(futures)):
try:
out.append(future.result())
except Exception as e:
out.append(e)
return out