-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_process_programming.py
More file actions
41 lines (31 loc) · 983 Bytes
/
Copy pathmulti_process_programming.py
File metadata and controls
41 lines (31 loc) · 983 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
29
30
31
32
33
34
35
36
37
38
39
40
41
import datetime
import multiprocessing
import math
def do_math(start=0, num=10):
# print(f'start: {start} ----------- num: {num} ////')
position = start
random_number = 1000 * 1000
avg = 0
while position < num:
position += 1
value = math.sqrt((position - random_number) ** 2)
avg += value / num
return int(avg)
def main():
t0 = datetime.datetime.now()
processor_count = multiprocessing.cpu_count()
pool = multiprocessing.Pool()
tasks = []
for n in range(1, processor_count + 1):
task = pool.apply_async(do_math, args=(50_000_000 * (n - 1) / processor_count, 50_000_000 * n / processor_count))
tasks.append(task)
pool.close()
pool.join()
dt = datetime.datetime.now() - t0
print(f'Done in {dt.total_seconds()} seconds')
# print('Our results:')
# for t in tasks:
# print(t.get())
if __name__ == '__main__':
print('Program has been started ...')
main()