0

I created a class that will generate multiple child processes, but do not want some of the parent process's objects to be copied over multiple child processes. For example, proc1 and proc2 in class x below copied their parent's variables 'a' and 'b' when they are started (i.e. proc1.start() and proc2.start()). However, I do not want 'a' to be copied, since those processes do not use it. In my program there are many of such variables that operate only in the parent process. Some of them are very large dictionaries or Pandas Dataframes. Is there a way to prevent them from being copied?

class x:
    def __init__(self):
        self.b = 20
        self.a = 10
        proc1 = mp.Process(target=self.func1)
        proc2 = mp.Process(target=self.func2)
        proc1.start()
        proc2.start()
        # Main process is doing something here
        proc1.join()
        proc2.join()

    def func1(self):
        time.sleep(10)
        print("Process 1:",self.b)

    def func2(self):
        self.b = 15
        print("Process 2:",self.b)

if __name__ == '__main__':
    y = x()
Navidad20
  • 832
  • 7
  • 11
Knoel
  • 13
  • 5

1 Answers1

0

Did you try taking the functions out of the class and then simply pass the variable?

def func1(b):
    time.sleep(10)
    print("Process 1:", b)

def func2(b):
    b = 15
    print("Process 2:", b)

class x:
    def __init__(self):
        self.b = 20
        self.a = 10
        proc1 = mp.Process(target=func1(self.b))
        proc2 = mp.Process(target=func2(self.b))
        proc1.start()
        proc2.start()
        # Main process is doing something here
        proc1.join()
        proc2.join()

if __name__ == '__main__':
    y = x()

Output:

Process 2: 15
Process 1: 20
Navidad20
  • 832
  • 7
  • 11