i'm trying experience multithreading in python. i'm using game it, i'm running strange synchronization issues create odd bugs in game. follows feel relevant code (the project 500 lines, didn't want overload of it)
i have game object controls aspects of ui , passing commands player object. plan create pool in main, , pass pool processevents() methods player/computer use large jobs.
import sys player import player ai import ai multiprocessing import pool, cpu_count class rtsgame(object): def processevents(self): self.player.processevents(self.ai.getships(), pool) self.ai.processevents(self.player.getunits(), pool) #end processevents if __name__ == '__main__': game = rtsgame() pool = pool(processes=cpu_count()) game.main()
eventually, pool object ends here:
def findtarget(self, targets, pool): ''' returns target best meeting search criteria. closest. add criteria strongest, weakest, or type. ''' results = pool.map(findtargethelper, map(lambda target: (self, target), targets)) shortest = min(results) if shortest[0] < self.attackinfo.getrange(): return shortest[1] #return ship @ shortest distance else: return none #return nothing ''' old code shortest = self.attackinfo.getrange() #you must within range of weapons shortesttarget = none distfunc = self.getdistance target in targets: dist = distfunc(self.position, target.getposition()) if dist < shortest: shortest = dist shortesttarget = target return shortesttarget ''' #end findtarget() def findtargethelper(target): #target[0] assumed 1 searching target return (target[0].getdistance(target[0].getposition(), target[1].getposition()), target[1])
please note findtargethelper() not method of object (to around pickleing problem). when using old code runs fine can take bit of time large amount of objects.
my problems strange. seems nothing being updated when objects being modified (through taking damage through attacks example).
i think i'm missing crucial in code make work. , want work, have other areas use in (drawing cpu intensive, example).
edit: i've been doing lots of research trying figure out. research has been unsuccessful. i'd appreciate can give!
Comments
Post a Comment