在Python的另一个类中运行多个进程后,如何在类列表中更改属性?

时间:2019-03-04 20:50:21

标签: python multiprocessing

我有两节课:

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得到更新。

谢谢大家

0 个答案:

没有答案