I have a file1 with code that increments the value of shared_var
once a second:
file1.py
import time
from multiprocessing import Value, Lock, Process
def update_shared_variable(shared_var, lock):
for i in range(100):
with lock:
shared_var.value = i # Update the value of the shared variable
time.sleep(1)
print("Updated value in file1:", shared_var.value)
if __name__ == '__main__':
shared_var = Value('i', 0)
lock = Lock()
update_process = Process(target=update_shared_variable, args=(shared_var, lock))
update_process.start()
update_process.join()
Result:
Updated value in file1: 0
Updated value in file1: 1
Updated value in file1: 2
Updated value in file1: 3
Updated value in file1: 4
Updated value in file1: 5
in file2 I'm trying, get the current shared_var value from file1 when it's running:
file2.py
import time
from multiprocessing import Value, Process
def get_current_value(shared_var):
while True:
current_value = shared_var.value
print("Current value of x from file1:", current_value)
time.sleep(2)
if __name__ == '__main__':
shared_var = Value('i', 0)
# Create a process to get the current value of x
get_value_process = Process(target=get_current_value, args=(shared_var,))
get_value_process.start()
get_value_process.join()
But I get this result:
Current value of x from file1: 0
Current value of x from file1: 0
Current value of x from file1: 0
Current value of x from file1: 0
Please tell me, what is the error here? And how to get the current shared_var value from file1 in file2(while file1 is running)?