2016-12-06 66 views
1

我有一個大計劃的一個小問題,所以我做了一個小例子,這說明我的問題:多重錯誤與結果

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__"。 我需要這個腳本的基本結構,所以請不要做太多的改變(如果可能的話)。

回答

0

問題是你想訪問的class1class1實例的屬性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) 
0

他的工作是我的機器蟒蛇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] 
+0

沒有對我的工作使用Python 2.7.12的Windows機器。在我的選擇中,您的回答應該是評論,而不是回答。 – martineau

+0

沒有足夠的聲望在你的問題下添加評論。 – oshaiken

+0

然後,也許你應該等到你的聲望足夠高時,,,它可能適合你,因爲你使用的是不同的操作系統(我猜測)。 – martineau