When the first process accesses
the object, it reads if the value is 0 it changes to 1
and when the second process accesses the same object it finds the value as 0 even though the first process changed it to 1. I just want them to work on the same list, but it seems that they are working on different copies of the object list. below is the code
import time
import random
from multiprocessing import Process
class foo:
flag_value = None
def __init__(self):
self.flag_value=0
def get_flag_value(self):
return self.flag_value
def set_flag_value(self,flag):
self.flag_value=flag
def proccess_fuction(self,pid,objects_list):
for index,object in enumerate(objects_list):
if (object.get_flag_value == 1):
print("proccess "+str(pid)+" read value at "+str(index)+" as :"+str(object.get_flag_value()))
time.sleep(random.uniform(1, 3))
else:
print("proccess "+str(pid)+" read value at "+str(index)+" as "+str(object.get_flag_value()))
object.set_flag_value(1)
print("proccess " + str(pid) + " set value at " + str(index) + " as " + str(object.get_flag_value()))
time.sleep(random.uniform(1, 3))
if __name__ == '__main__':
list_of_objects=list()
for n in range (7):
list_of_objects.append(foo())
p1 = Process(target=proccess_fuction,args=(None,1,list_of_objects))
p2 = Process(target=proccess_fuction, args=(None,2,list_of_objects))
p1.start()
time.sleep(3)
p2.start()
p1.join()
p2.join()