1

I have a certain class that has an attribute list. There are some functions in the class that write to, but never read from this list. I initialize the class with a list and call the functions from multiple threads, however after waiting for all threads to finish the list remains empty.

The value order in the list does not matter.

from multiprocessing import Process

class TestClass():

    def __init__(self, vals):
        self.vals = vals
        
    def linearrun(self):
        Is = range(2000)
        Js = range(2000)
        
        for i in Is:
            for j in Js:
                self.vals.append(i+j)

if __name__ == "__main__":
    vals = []
    instantiated_class = TestClass(vals)
    processes = []

    for _ in range(10):
        new_process=Process(target=instantiated_class.linearrun)
        processes.append(new_process)
        new_process.start()
    
    for p in processes:
        p.join()

    print(vals)
  • 1
    whenever you spawn a new process, python copies the objects needed to run the function, in your case the 10 process you are creating creates 10 copies of each `instantiated_class` and the list attribute `self.vals` – python_user May 03 '21 at 04:36
  • might want to look into https://docs.python.org/3/library/multiprocessing.html#pipes-and-queues if you want to communicate between processes – python_user May 03 '21 at 04:38
  • Thanks, that is helpful to know. Do pipes work for parent-child connection as well? – Ballistic_Beagle May 03 '21 at 04:39
  • https://stackoverflow.com/questions/10721915/shared-memory-objects-in-multiprocessing or https://stackoverflow.com/questions/15857838/modify-object-in-python-multiprocessing seems to be a good start – python_user May 03 '21 at 04:41
  • 1
    Awesome, that is exactly what I am looking for. Thanks for your help @python_user! – Ballistic_Beagle May 03 '21 at 04:47

0 Answers0