结果的多处理错误
Multiprocessing Error with Results
我在一个大程序中遇到了一个小问题,所以我做了一个小例子,说明了我的问题:
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)
当我使用最后一行时 resultslist = list(results)
我得到以下错误:
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'
命令 class1.classfunction1(x)
和 class2.classfunction2(y)
必须在 if__name__=="__main__"
中。
我需要这个脚本的基本结构,所以请不要做太多的改变(如果可能的话)。
在我的机器上运行 python 2.7.10
('This variable is callable', 1)
('And this one is also callable', 2)
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
问题是您正在尝试访问从未在工作子进程中创建的 class1
实例的 x
属性,因为 class1.classfunction1()
没有被调用。
为了解决这个问题,我添加了一个 init()
函数并在主进程中调用它,并将它指定为 initializer
函数用于 Pool
创建的每个子进程:
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)
我在一个大程序中遇到了一个小问题,所以我做了一个小例子,说明了我的问题:
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)
当我使用最后一行时 resultslist = list(results)
我得到以下错误:
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'
命令 class1.classfunction1(x)
和 class2.classfunction2(y)
必须在 if__name__=="__main__"
中。
我需要这个脚本的基本结构,所以请不要做太多的改变(如果可能的话)。
在我的机器上运行 python 2.7.10
('This variable is callable', 1)
('And this one is also callable', 2)
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
I'm in the Testfunction
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
问题是您正在尝试访问从未在工作子进程中创建的 class1
实例的 x
属性,因为 class1.classfunction1()
没有被调用。
为了解决这个问题,我添加了一个 init()
函数并在主进程中调用它,并将它指定为 initializer
函数用于 Pool
创建的每个子进程:
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)