Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing help
#6
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()
Reply


Messages In This Thread
Multiprocessing help - by tareq_87 - Oct-23-2019, 10:44 AM
RE: Multiprocessing help - by buran - Oct-23-2019, 10:47 AM
RE: Multiprocessing help - by tareq_87 - Oct-24-2019, 01:40 PM
RE: Multiprocessing help - by buran - Oct-24-2019, 01:45 PM
RE: Multiprocessing help - by micseydel - Oct-28-2019, 11:25 PM
RE: Multiprocessing help - by tareq_87 - Oct-29-2019, 06:13 PM
RE: Multiprocessing help - by micseydel - Oct-29-2019, 10:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020