python 多处理模块更新被调用进程内的 class 属性,但不会全局更新
python multiprocessing module updates a class attribute inside the called process but is not updated globally
使用我的多处理代码
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 属性时,该值应该在那里,对吗?或者我应该尝试 return 它然后在 join() 之后连接?
一个简单的解释会很可爱。
我通过使用进程管理器并将该管理器放在 class 之外来修复它。
manager = multiprocessing.Manager()
course_list = manager.list()
class x:
#append things to course_list
使用我的多处理代码
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 属性时,该值应该在那里,对吗?或者我应该尝试 return 它然后在 join() 之后连接? 一个简单的解释会很可爱。
我通过使用进程管理器并将该管理器放在 class 之外来修复它。
manager = multiprocessing.Manager()
course_list = manager.list()
class x:
#append things to course_list