0

for testing reasons I start only 1 process. One given argument is an array that shall be changed from that process.

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)

I try to write 2.4 directly in the list, but no changing is shown when calling "printctypemovelist". I set "Ready" to True and it works. I used information from http://docs.python.org/2/library/multiprocessing.html#module-multiprocessing.sharedctypes

I hope someone can find my mistake, if it is too difficult to read, please let me know.

4

1 回答 1

0

问题是您正在尝试共享一个普通的 Python 列表:

ctML = []

改用代理对象:

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

有关更多详细信息,请参阅有关进程之间共享状态的Python 文档。

于 2013-10-08T16:41:33.177 回答