2013-10-07 80 views
0

出於測試原因我只開始1個進程。一個給定的參數是應該從該過程改變的數組。python多處理sharedctype數組 - 不能寫

class Engine(): 
Ready = Value('i', False) 

def movelisttoctypemovelist(self, movelist): 
    ctML = [] 
    for zug in movelist: 
     ctZug = ctypeZug() 
     ctZug.VonReihe = zug.VonReihe 
     ctZug.VonLinie = zug.VonLinie 
     ctZug.NachReihe = zug.NachReihe 
     ctZug.NachLinie = zug.NachLinie 
     ctZug.Bewertung = zug.Bewertung 
     ctML.append(ctZug) 
    return ctML 

def findbestmove(self, board, settings, enginesettings): 
    print ("Computer using", multiprocessing.cpu_count(),"Cores.") 
    movelist = Array(ctypeZug, [], lock = True) 
    movelist = self.movelisttoctypemovelist(board.movelist) 
    bd = board.boardtodictionary() 
    process = [] 
    for i in range(1): 
     p = Process(target=self.calculatenullmoves, args=(bd, movelist, i, self.Ready)) 
     process.append(p) 
     p.start() 
    for p in process: 
     p.join() 
    self.printctypemovelist(movelist, settings) 
    print ("Ready:", self.Ready.value) 

def calculatenullmoves(self, boarddictionary, ml, processindex, ready): 
    currenttime = time() 
    print ("Process", processindex, "begins to work...") 
    board = Board() 
    board.dictionarytoboard(boarddictionary) 
    ... 
    ml[processindex].Bewertung = 2.4 
    ready.value = True 
    print ("Process", processindex, "finished work in", time()-currenttime, "sec") 

def printctypemovelist(self, ml): 
    for zug in ml: 
     print (zug.VonReihe, zug.VonLinie, zug.NachReihe, zug.NachLinie, zug.Bewertung) 

我嘗試直接在列表中寫入2.4,但調用「printctypemovelist」時未顯示更改。 我將「Ready」設置爲True,它可以工作。 我使用的信息來自http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.sharedctypes

我希望有人能找到我的錯誤,如果太難閱讀,請告訴我。

回答

0

的問題是,你想分享一個普通的Python列表:

ctML = [] 

使用,而不是代理對象:

from multiprocessing import Manager 
ctML = Manager().list() 

見Python的文檔上Sharing state between processes瞭解更多詳情。