Search code examples
pythonpython-multiprocessingpython-class

python multiprocessing module updates a class attribute inside the called process but is not updated globally


with my multiprocessing code

    def __get_courses_per_moderator(self, moderator, page=None):
        print("started")
        try:
            response = self.class_service.courses().list(pageSize=0,
                                                         courseStates='ACTIVE', 
            teacherId=moderator, pageToken=None).execute()
            for data in response["courses"]:
                self.courses_in_classroom.append(data["name"])
            print(self.courses_in_classroom) # THE modifications are clear

        except Exception as e:
            print("__get_courses_per_moderator ERROR-{}".format(e))

    def get_courses_from_classroom(self):
        # batch = service.new_batch_http_request(callback=self.__callback)
        pool = []
        for email in self.additional_emails:
            try:
                process = multiprocessing.Process(
                    target=self.__get_courses_per_moderator, args=[email])
                process.start()
                pool.append(process)
            except Exception as e:
                print("get_courses_from_classroom ERROR-{}".format(e))

        for process in pool:
            process.join()
            print("joined")
        print(self.courses_in_classroom) # the attribute is empty.

As far as I understand python is synchronous. So when the processes update the class attribute, the value should be there right? or should I try to return it and then concat after join() ? A simple explanation would be lovely.


Solution

  • I fixed it by using a process manager and putting that manager outside of the class.

    manager = multiprocessing.Manager()
    course_list = manager.list()
    class x:
        #append things to course_list