在Python documentation on multiprocessing中有许多并行化函数工作的例子。我假设也可以对类中包含的函数执行此操作。但是,以下示例不起作用。 它产生的进程计算当前进程数的2的乘积。报告对象内部的计算值有效,但是当我尝试在作业完成后获取计算值时,它只会报告构造函数中设置的值。
班级定义
import multiprocessing
class MyClass():
def __init__(self,runname):
self.runname = runname
self.output = 0
def calculate(self,input):
self.output = input*2
print "Reporting from runname %s, calculation yielded %s" % (self.runname,self.output)
def getOutput(self):
return self.output
调用对象的代码:
objectList = [] #Store objects
jobList = [] #Store multiprocessing objects
#Run the workers in 4 parallel processes
for i in range(4):
thisRunname = 'Worker:%s' % i
thisInstance = MyClass(thisRunname)
p = multiprocessing.Process(target=thisInstance.calculate, args=(i,))
jobList.append(p)
p.start()
objectList.append(thisInstance)
for thisJob in jobList: #Wait till all jobs are done
thisJob.join()
print "Jobs finished"
for thisInstance in objectList:
print "Worker %s calculated %s " % (thisInstance.runname,thisInstance.getOutput() )
输出:
Reporting from runname Worker:0, calculation yielded 0
Reporting from runname Worker:1, calculation yielded 2
Reporting from runname Worker:2, calculation yielded 4
Reporting from runname Worker:3, calculation yielded 6
Jobs finished
Worker Worker:0 calculated 0
Worker Worker:1 calculated 0
Worker Worker:2 calculated 0
Worker Worker:3 calculated 0
因此计算函数可以毫无问题地生成,当尝试检索计算值时,它只返回0,它在构造函数中设置为值。
我是否缺少一个关键概念,如何获得self.output值?
答案 0 :(得分:1)
Process
类提供的序列化只是单向的。它会序列化您提供的target
和args
,但它不会自动带回任何内容。
因此,当您创建Process
es时,multiprocessing
模块会挑选您创建的MyClass
个实例(因为target
是实例的绑定方法)并且每个人都会在其中一个子进程中对其进行unpickled。这就是为什么每个孩子都按照您的预期进行计算的原因。
但是,对子进程的实例版本的更改不会被复制回主进程。没有机制可以做到这一点。最后,当子进程结束时,实例会被丢弃。父进程的MyClass
实例未更新,这就是您看到calculated 0
消息的原因。