-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
28 lines (20 loc) · 718 Bytes
/
Copy pathmain.py
File metadata and controls
28 lines (20 loc) · 718 Bytes
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
import multiprocessing
from tqdm import tqdm
def function_to_parallelize(params):
param1 = params['param1']
param2 = params['param2']
result = {}
result['sum'] = param1+param2
return result
# This chooses all cores except 2, unless there are only two or less cores.
NCPU = multiprocessing.cpu_count() - 2 if multiprocessing.cpu_count() > 2 else 1
X_values = [10,22,35,4,532,12,42,53,23]
params = ({
'param1': x,
'param2': 15
} for x in X_values)
with multiprocessing.Pool(processes=NCPU) as p:
MAX_COUNT = len(X_values)
for res in tqdm(p.imap(function_to_parallelize, params),total=MAX_COUNT):
if res is not None:
print(res['sum'])