0
我在模塊多處理中遇到了一個Python問題。 基本上,我做了一個遊戲(用tkinter作爲圖形),其中我有一個類遊戲和幾個類(entites),都有一個更新(自我)方法。 所以這是一個有點像:Python - 類與多處理的集成
class Game:
__init__(self, etc...):
self.entities = []
gameloop(self):
for entity in self.entities:
entity.update
class EntityExample:
__init__(self, game, etc...):
self.game = game
update(self):
#stuff
然後我做的:
game = Game()
game.entities.append(EntityExample())
game.gameloop()
所以,我想,對代碼進行優化,做這樣的事情:
進口多處理
class Game:
__init__(self, etc...):
self.entities = []
self.threads = []
self.lock = multiprocessing.Lock()
gameloop(self):
for entity in self.entities:
entity.update
class EntityExample:
__init__(self, game, etc...):
self.game = game
update(self):
self.game.lock.acquire()
#stuff
self.game.lock.release()
而且在gameloop:
for entity in entities:
t = multiprocessing.Process(target=entity.update)
t.start()
t.join
self.threads.append(t)
的目標是在同一時間,以提高性能上做不同的內核的計算,但它並不可悲工作。 我還要求在IDLE中殺死程序:「程序仍在運行,你想殺死它嗎?」。
由於提前,
Talesseed
附: :班級不可採
P.P.S. :我讀過創建一個新的Process將文件內的代碼複製到一個新的線程,這可能是一個問題,因爲我的代碼長達1600行。
爲什麼所有的代碼都以連字符開頭? –
@BryanOakley糟糕,我會刪除那 – Talesseed
'__init__',gameloop或更新已被聲明爲函數,並且update缺少自己,因此不是EntityExample的一部分 –