我有两节课:
from multiprocessing import Process
class A(object):
def __init__(self, name, time):
self.name = name
self.time = time
class Worker(object):
def __init__(self, names):
self.list_A = [A(names[i], "init_time") for i in len(names)]
def work(self):
jobs = []
for a in self.list_A:
j = Process(target=self._mini, args=(a,))
jobs.append(j)
for job in jobs:
job.start()
for job in jobs:
job.join()
def _mini(self, a):
#do something here, eventually compute new_time
#change the time of class a:
a.time = new_time
names = ["asd", "dfg", "rty"]
W = Worker(names)
W.work()
for name in W.list_A:
print(name.time)
#this should be the updated time
在处理之后,我希望W.list_A
的成员(它们是在Worker类的__init__
中初始化的类A)的属性self.time
得到更新。
谢谢大家