Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing help
#1
Hi

I have a list of objects that I need to send it to multiple process functions to work on. it is working well from multiprocessing perspectives. However, it seems that they are not working on the same object. Am I missing something?? any suggestions would be appreciated..
Reply
#2
(Oct-23-2019, 10:44 AM)tareq_87 Wrote: any suggestions would be appreciated..
post your code in python tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Well Thanks for your reply. it was wonderful...
Reply
#4
(Oct-24-2019, 01:40 PM)tareq_87 Wrote: Well Thanks for your reply. it was wonderful...
well, I don't see what help you expect if we don't see your code...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
+1 to it being difficult/impossible to provide help without the question asker's code.
Reply
#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
#7
They're separate processes, so they don't share memory, so it shouldn't be surprising that changes from one process aren't reflected in the other process. I'm no expert on writing code like this but I recommend you checkout this.

That said...
You can omit line of 7 entirely.
It's considered bad style to use variable names like "object" or "str" because they're defined as built-ins.
You can get rid of self on line 15 and the Nones on lines 32 and 33.
Line 18 is comparing a method to a number. You probably intend to call the method (with parenthesis), not compare to it.
There may be things others point out, I'm at work and about to head into a meeting so I'll call it here for now.
Reply


Forum Jump:

User Panel Messages

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