def __init__(self, controllermodule, config, q): """ Instantiates a new Controller. :param controllermodule: A controller instance. :param config: The parsed json defining a given patch :param q: The message queue assigned to this worker """ signal.signal(signal.SIGINT, signal.SIG_IGN) self.config = config self.q = q self.controller = controllermodule.Controller(config) self.clocked = isinstance(self.controller, BaseClockedController) # Setup the thread we are on self.configure_thread() #TODO: sync start time across threads # Start absolute system time self.t = precision_timer.getabsolutetime() logservice.log(logservice.LOG_DEBUG, 'Start time for controller worker {0:.6f}'.format(self.t)) self.next_t = self.t
def run(self): """ Main entry point for the controller worker process. """ while 1: # Get current time self.t = precision_timer.getabsolutetime() # Perform periodic work, clocked or not if self.clocked: # Calculate next t if self.t >= self.next_t: self.controller.clock(self.t) self.calculate_next_t() # if next_t is more than 100ms, sleep for a bit if self.next_t - self.t > 0.025: precision_timer.absolutewait(self.t + 0.025) else: precision_timer.absolutewait(self.next_t) self.controller.loop(self.t) else: self.controller.loop(self.t) precision_timer.absolutewait(self.t + 0.025) # See if any commands are in the queue message = self.get_next_message() if message is not None: if message["type"] == controllermodel.MESSAGE_KILL: # Tell the controller to release resources self.controller.release() # And goodbye # TODO: this isnt exiting return 0 else: self.controller.message(message)