Python多处理SharedType数组-无法写入
出于测试原因,我只启动一个进程。一个给定的参数是一个数组,该数组将从该过程中更改Python多处理SharedType数组-无法写入,python,multiprocessing,Python,Multiprocessing,出于测试原因,我只启动一个进程。一个给定的参数是一个数组,该数组将从该过程中更改 class Engine(): Ready = Value('i', False) def movelisttoctypemovelist(self, movelist): ctML = [] for zug in movelist: ctZug = ctypeZug() ctZug.VonReihe = zug.VonReihe ctZug.Von
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,它就工作了。
我使用了来自
我希望有人能发现我的错误,如果它太难阅读,请告诉我。问题是您试图共享一个简单的Python列表:
ctML = []
请改用代理对象:
from multiprocessing import Manager
ctML = Manager().list()
有关更多详细信息,请参见上的Python文档