3 # Hacky alternative to using select for timing out players
5 # WARNING: Do not wrap around HumanPlayer or things breakify
7 class Sleeper(multiprocessing.Process):
8 def __init__(self, timeout):
9 multiprocessing.Process.__init__(self)
10 self.timeout = timeout
13 time.sleep(self.timeout)
16 class Worker(multiprocessing.Process):
17 def __init__(self, function, args, q):
18 multiprocessing.Process.__init__(self)
19 self.function = function
24 #print str(self) + " runs " + str(self.function) + " with args " + str(self.args)
25 self.q.put(self.function(*self.args))
29 def TimeoutFunction(function, args, timeout):
30 q = multiprocessing.Queue()
31 w = Worker(function, args, q)
35 while True: # Busy loop of crappyness
40 #print "TimeoutFunction gets " + str(result)
42 elif not s.is_alive():
45 raise Exception("UNRESPONSIVE")
50 # A player that wraps another player and times out its moves
52 # A (crappy) alternative to the use of select()
53 class TimeoutPlayer(Player):
54 def __init__(self, base_player, timeout):
55 Player.__init__(self, base_player.name, base_player.colour)
56 self.base_player = base_player
57 self.timeout = timeout
60 return TimeoutFunction(self.base_player.select, [], self.timeout)
64 return TimeoutFunction(self.base_player.get_move, [], self.timeout)
66 def update(self, result):
67 return TimeoutFunction(self.base_player.update, [result], self.timeout)
69 def quit(self, final_result):
70 return TimeoutFunction(self.base_player.quit, [final_result], self.timeout)