-
Notifications
You must be signed in to change notification settings - Fork 7
/
multi_processing.py
111 lines (84 loc) · 2.61 KB
/
multi_processing.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import time
import multiprocessing
def calc_square(numbers, result):
print('Calculating square numbers')
for number in numbers:
time.sleep(0.2)
print('square', number * number)
def calc_cube(numbers, result, value, queue):
print('Calculate cube of numbers')
value.value = 5
for index, number in enumerate(numbers):
time.sleep(0.2)
queue.put(number * number * number)
result[index] = number * number * number
def main():
# SHARED MEMORY CONCEPT
arr = [2, 3, 8, 9]
start_time = time.time()
# We have to create a shared memory variable. Specify data type and size
result = multiprocessing.Array('i', 4)
value = multiprocessing.Value('i', 0)
queue = multiprocessing.Queue()
# p1 = multiprocessing.Process(target=calc_square, args=(arr,))
p2 = multiprocessing.Process(target=calc_cube, args=(arr, result, value, queue))
# p1.start()
p2.start()
# p1.join()
p2.join()
while not queue.empty():
print('Getting')
print(queue.get())
print(result[:])
print(value.value)
print("done in: {}".format(time.time() - start_time))
print('Done with everything')
import time
import multiprocessing
def deposit(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value + 1
lock.release()
def withdraw(balance, lock):
for i in range(100):
time.sleep(0.01)
lock.acquire()
balance.value = balance.value - 1
lock.release()
def main2():
# LOCK CONCEPT
balance = multiprocessing.Value('i', 200)
# Lock is used to lock a variable so that the value isn't frozen in time and when it is changed, it is not changed fast
# enough for the other prcoess to know the updated value thus using the old value of the variable
lock = multiprocessing.Lock()
d = multiprocessing.Process(target=deposit, args=(balance, lock))
w = multiprocessing.Process(target=withdraw, args=(balance, lock))
d.start()
w.start()
d.join()
w.join()
print(balance.value)
from multiprocessing import Pool
def f(n):
return n * n
def main3():
# MAP AND REDUCE CONCEPT
array = [1, 2, 3, 4, 5, 6]
start_time = time.time()
p = Pool()
result = p.map(f, array)
end_time = time.time() - start_time
print(result, end_time)
start_time = time.time()
squared = []
for n in array:
time.sleep(5)
squared.append(n * n)
end_time = time.time() - start_time
print(squared, end_time)
if __name__ == '__main__':
# main()
# main2()
main3()