python多重处理模块会在被调用的进程内更新类属性,但不会全局更新

问题描述 投票:0回答:1

带有我的多处理代码

    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.

据我了解,python是同步的。因此,当进程更新class属性时,该值应该正确吗?还是我应该尝试将其返回,然后在join()之后进行concat?一个简单的解释将很可爱。

python python-multiprocessing python-class
1个回答
0
投票
我通过使用流程管理器并将该管理器放在类之外来解决。

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

© www.soinside.com 2019 - 2024. All rights reserved.