-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_shared_state.py
More file actions
68 lines (54 loc) · 1.95 KB
/
8_shared_state.py
File metadata and controls
68 lines (54 loc) · 1.95 KB
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
from multiprocessing import Process, Value, current_process, Lock
import os
from concurrent.futures import ProcessPoolExecutor, as_completed
import time
def add_one_not_sharing(v: int, process: int):
process_name = current_process().name
pid = os.getpid()
for _ in range(10000):
v += 1
print(f"Process:{process}, PID: {pid}, PName: {process_name}, Value = {v}\n", end="")
print(f"Process:{process}, PID: {pid}, PName: {process_name} Done")
def add_one_sharing(v: Value, process: int, lock):
process_name = current_process().name
pid = os.getpid()
for _ in range(10000):
lock.acquire()
v.value += 1
lock.release()
print(f"Process:{process}, PID: {pid}, PName: {process_name}, Value = {v.value}\n", end="")
print(f"Process:{process}, PID: {pid}, PName: {process_name} Done")
def main():
# Shared X
# value = 0
#
# with ProcessPoolExecutor() as executor:
# futures = [executor.submit(add_one_not_sharing(value, i)) for i in range(1,11)]
#
# for future in as_completed(futures):
# future.done()
#
# print(f"Final Value = {value}")
# Shared O with Lock
# value = Value('i', 0)
#
# with ProcessPoolExecutor() as executor:
# futures = [executor.submit(add_one_sharing(value, i)) for i in range(1,11)]
#
# for future in as_completed(futures):
# future.done()
# Shared O without Lock
""" As You can see, if there is no lock, the value might be the case as you expected"""
value = Value('i', 0)
# If you want to use Lock, Uncomment lock features in the add_one_sharing function
lock = Lock()
processes = []
for i in range(1, 11):
p = Process(target=add_one_sharing, args=(value, i, lock))
processes.append(p)
p.start()
for process in processes:
process.join()
print(f"Final Value = {value.value}")
if __name__ == "__main__":
main()