-
Notifications
You must be signed in to change notification settings - Fork 1
/
5_thread_process_pool.py
51 lines (39 loc) · 1.35 KB
/
5_thread_process_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
# CPU-bound process (computationally expensive task)
'''
Pool is an easier approach to Process management as it handles
the creation of processes, the allocation of tasks among them and
the collection of each stream result in a final output
input task
task 1
task 2
task 3
map on n cpu
/ | \
t1 t2 t3 # run()
\ | /
reduce outputs
'''
import os
import time
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
x=10000000
class CalculateSquare:
''' no longer a Process but a task to use in the map '''
def run(self):
s=0
for n in range(x):
s+=n*n
return s
if __name__ == "__main__":
# create one task for each CPU core
tasks = [CalculateSquare() for cpu in range(os.cpu_count())]
pool = Pool(os.cpu_count()) # pool creates a separate process for each CPU core [thread: ThreadPool(os.cpu_count())]
start = time.time()
# pool get each task in the iterable and push it into an available process which executes the function
task_output = pool.map(CalculateSquare.run, tasks) # map method accepts a function and an iterable and returns a list
pool.close()
pool.join()
end = time.time()
print(f'Work took {end - start} seconds')
print(f'Sum tasks output: {sum(x for x in task_output)} ')