3 # Hacky alternative to using select for timing out players
5 # WARNING: Do not wrap around HumanPlayer or things breakify
6 # WARNING: Do not use in general or things breakify
8 class Sleeper(multiprocessing.Process):
9 def __init__(self, timeout):
10 multiprocessing.Process.__init__(self)
11 self.timeout = timeout
14 time.sleep(self.timeout)
17 class Worker(multiprocessing.Process):
18 def __init__(self, function, args, q):
19 multiprocessing.Process.__init__(self)
20 self.function = function
25 #print str(self) + " runs " + str(self.function) + " with args " + str(self.args)
27 self.q.put(self.function(*self.args))
33 def TimeoutFunction(function, args, timeout):
34 q = multiprocessing.Queue()
35 w = Worker(function, args, q)
39 while True: # Busy loop of crappyness
44 #print "TimeoutFunction gets " + str(result)
46 elif not s.is_alive():
49 raise Exception("TIMEOUT")
54 # A player that wraps another player and times out its moves
56 # A (crappy) alternative to the use of select()
57 class TimeoutPlayer(Player):
58 def __init__(self, base_player, timeout):
59 Player.__init__(self, base_player.name, base_player.colour)
60 self.base_player = base_player
61 self.timeout = timeout
64 return TimeoutFunction(self.base_player.select, [], self.timeout)
68 return TimeoutFunction(self.base_player.get_move, [], self.timeout)
70 def update(self, result):
71 return TimeoutFunction(self.base_player.update, [result], self.timeout)
73 def quit(self, final_result):
74 return TimeoutFunction(self.base_player.quit, [final_result], self.timeout)