-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiprocessing_pool.py
63 lines (48 loc) · 1.37 KB
/
multiprocessing_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
from multiprocessing.dummy import Pool
import numpy as np
import time
# calculus thread
def my_function(i, param1, param2, param3):
result = param1 ** 2 * param2 + param3
time.sleep(2)
return (i, result)
def get_result(result):
global results
results.append(result)
# Returning name thread
def worker(nome):
return nome
def callback(nome):
print(nome)
def main_thread(nome):
pool_threads= 8
div = 10
try:
with Pool(pool_threads) as pool:
# for i in range(1 , div):
re = pool.apply_async(worker, args=[nome])
re.get()
pool.close()
pool.join()
except Exception as error:
print(error)
if __name__ == "__main__":
params = "Johnatas"
main_thread(params)
params = np.random.random((10, 3)) * 100.0
results = []
ts = time.time()
for i in range(0, params.shape[0]):
get_result(my_function(i, params[i, 0], params[i, 1], params[i, 2]))
print('Time in serial:', time.time() - ts)
print(results)
results = []
ts = time.time()
pool = mp.Pool(mp.cpu_count())
for i in range(0, params.shape[0]):
pool.apply_async(my_function, args=(i, params[i, 0], params[i, 1], params[i, 2]), callback=get_result)
pool.close()
pool.join()
print('Time in parallel:', time.time() - ts)
print(results)
print(mp.cpu_count())