带处理结果的多处理错误 [英] Multiprocessing Error with Results
问题描述
我在一个大程序中遇到了一个小问题,所以我做了一个小例子,它显示了我的问题:
import多重处理
$ b $ class class1():
def classfunction1(self,a):
self.x = a
$ b $ class class2():
def classfunction2(self,a):
self.y = a
def test(i):
print(我在测试函数中)
b = i * class1.x * class2.y
return b
class1 = class1()
class2 = class2()
if __name__ ==__main__:
x = 1
y = 2
class1.classfunction1(x)
class2.classfunction2(y)
print(This变量是可调用的,class1.x)
print(这个也是可调用的,class2.y)
counter = []
我在范围内(10):
counter.append(i)
pool = multiprocessing.Pool(processes = 4)
results = pool.imap(test,counter)
pool.close()
pool .join()
#resultslist = list(结果)
当我使用最后一行 resultslist = list(results)
我得到以下错误:
multiprocessing.pool.RemoteTraceback:
$ bTraceback(最近一次调用最后一次):
File C:\ Program Files \Anconda3\lib\multiprocessing\pool.py,第119行,在worker
result =(True,func(* args,** kwds))
文件C:\Users\example2.py,第24行,在测试中
b = i * class1.x * class2.y
AttributeError:'class1'对象没有属性'x'
上述异常是以下异常的直接原因:
Traceback(最近一次调用的最后一个):
在< module>文件中的第43行C:\Users\example2.py
resultslist = list(results)
文件C:\程序文件\Anconda3\lib\multiprocessing\pool.py,第695行,在下一个
中增加值
AttributeError:'class1'对象没有属性'x'
class1.classfunction1(x)
和class2.classfunction2(y)
位于if__name __ = = __主__
。
我需要这个脚本的基本结构,所以请不要做太多的修改(如果可能的话)。
解决方案问题在于你试图访问
(!),因为
class1
实例的x
属性> class1class1.classfunction1()
没有在它们中被调用,所以在worker子进程中从未创建过。 为了解决这个问题,我添加了一个init()
函数,并在主进程中调用它并指定它作为由Pool
创建的每个子进程的初始化程序
函数:import multiprocessing
$ b $ class class1():
def classfunction1(self,a):
self.x = a
$ b $ class class2():
def classfunction2(self,a):
self.y = a
def test(i):
print(我在测试函数中)
b = i * class1.x * class2.y
return b
def init(): #增加
全球class1,class2
class1 = class1()
class2 = class2()
x = 1
y = 2
class1.classfunction1(x)
class2.classfunction2(y)
if __name__ ==__main__:
init()#显式调用
print(This variable is callable, class1.x)
print(这个也可以调用,class2.y)
(10):
counter.append(i)
pool = multiprocessing.Pool(initializer = init,processes = 4)#隐式调用
results = pool.imap(test,counter)
pool.close()
pool.join()
resultlist = list(results)
print(resultslist)
I have a small problem in a big program, so I made a small example, which shows my problem:
import multiprocessing class class1(): def classfunction1(self, a): self.x = a class class2(): def classfunction2(self, a): self.y = a def test(i): print("I'm in the Testfunction") b = i * class1.x * class2.y return b class1 = class1() class2 = class2() if __name__ == "__main__": x = 1 y = 2 class1.classfunction1(x) class2.classfunction2(y) print("This variable is callable", class1.x) print("And this one is also callable", class2.y) counter = [] for i in range(10): counter.append(i) pool = multiprocessing.Pool(processes=4) results = pool.imap(test, counter) pool.close() pool.join() #resultslist = list(results)
When I use the last line
resultslist = list(results)
I get the follow Error:multiprocessing.pool.RemoteTraceback:
Traceback (most recent call last): File "C:\Program Files\Anaconda3\lib\multiprocessing\pool.py", line 119, in worker result = (True, func(*args, **kwds)) File "C:\Users\example2.py", line 24, in test b = i * class1.x * class2.y AttributeError: 'class1' object has no attribute 'x' The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "C:\Users\example2.py", line 43, in <module> resultslist = list(results) File "C:\Program Files\Anaconda3\lib\multiprocessing\pool.py", line 695, in next raise value AttributeError: 'class1' object has no attribute 'x'
It is necessary that the commands
class1.classfunction1(x)
andclass2.classfunction2(y)
are in theif__name__=="__main__"
. I need the basic structure of this script, so please don't do too many changes (if possible).解决方案The problem is you're trying to access the
x
attribute of theclass1
instance ofclass1
(!) that has never been created in the worker subprocesses becauseclass1.classfunction1()
isn't called in them.To fix that issue I added an
init()
function and call it both in the main process and specify it as theinitializer
function for each of the subprocesses created by thePool
:import multiprocessing class class1(): def classfunction1(self, a): self.x = a class class2(): def classfunction2(self, a): self.y = a def test(i): print("I'm in the Testfunction") b = i * class1.x * class2.y return b def init(): # added global class1, class2 class1 = class1() class2 = class2() x = 1 y = 2 class1.classfunction1(x) class2.classfunction2(y) if __name__ == "__main__": init() # explicit call here print("This variable is callable", class1.x) print("And this one is also callable", class2.y) counter = [] for i in range(10): counter.append(i) pool = multiprocessing.Pool(initializer=init, processes=4) # implicit call results = pool.imap(test, counter) pool.close() pool.join() resultslist = list(results) print(resultslist)
这篇关于带处理结果的多处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!