2013-10-07 5 views
0

pour des raisons de test Je ne démarre qu'un seul processus. Un argument donné est un tableau qui doit être modifié à partir de ce processus.Python multitraitement sharedctype array - ne peut pas écrire

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) 

J'essaie d'écrire 2.4 directement dans la liste, mais pas de changement est représentée lors de l'appel « printctypemovelist ». Je définis "Prêt" sur Vrai et cela fonctionne. J'ai utilisé des informations de http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.sharedctypes

J'espère que quelqu'un peut trouver mon erreur, si c'est trop difficile à lire, s'il vous plaît faites le moi savoir.

Répondre

0

Le problème est que vous essayez de partager une liste Python simple:

ctML = [] 

Utilisez un objet proxy à la place:

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

Voir Python doc sur Sharing state between processes pour plus de détails.