我有一個大計劃的一個小問題,所以我做了一個小例子,這說明我的問題:多重錯誤與結果
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.12的Windows機器。在我的選擇中,您的回答應該是評論,而不是回答。 – martineau
沒有足夠的聲望在你的問題下添加評論。 – oshaiken
然後,也許你應該等到你的聲望足夠高時,,,它可能適合你,因爲你使用的是不同的操作系統(我猜測)。 – martineau