def run(self): self.last_recorded_time = time() super(ThreadsControllerBase, self).start() last_iteration_time = 0.0 while True: self.input_condition.acquire() self.handleInput(last_iteration_time) self.input_condition.release() #Compute the new state based on internal events self.object_manager.stepAll(last_iteration_time) self.handleWaiting() self.input_condition.acquire() if self.stop_thread: break self.input_condition.release() previous_recorded_time = self.last_recorded_time self.last_recorded_time = time() last_iteration_time = self.last_recorded_time - previous_recorded_time
def run(self): self.last_recorded_time = time() super(ThreadsControllerBase, self).start() last_iteration_time = 0.0 while True: self.input_condition.acquire() self.handleInput(last_iteration_time) self.input_condition.release() #Compute the new state based on internal events self.object_manager.stepAll(last_iteration_time) self.handleWaiting() self.input_condition.acquire() if self.stop_thread : break self.input_condition.release() previous_recorded_time = self.last_recorded_time self.last_recorded_time = time() last_iteration_time = self.last_recorded_time - previous_recorded_time
def handleWaiting(self): self.input_condition.acquire() wait_time = self.getWaitTime() if (wait_time <= 0.0): return if wait_time == INFINITY: if self.keep_running: self.input_condition.wait() #Wait for a signals else: self.stop_thread = True elif wait_time != 0.0: reduced_wait_time = wait_time - (time() - self.last_recorded_time) if reduced_wait_time > 0.0: self.input_condition.wait(reduced_wait_time) self.input_condition.release()
def handleWaiting(self): self.input_condition.acquire() wait_time = self.getWaitTime() if(wait_time <= 0.0): return if wait_time == INFINITY : if self.keep_running : self.input_condition.wait() #Wait for a signals else : self.stop_thread = True elif wait_time != 0.0 : reduced_wait_time = wait_time - (time() - self.last_recorded_time) if reduced_wait_time > 0.0 : self.input_condition.wait(reduced_wait_time) self.input_condition.release()
def nextDelta(self): now = time() if self.next_wakeup: simulated_now = self.next_wakeup else: simulated_now = now if now - self.last_print > 1.0: behind_schedule = now - simulated_now if behind_schedule > 0.1: print "Warning: running %.f ms behind schedule" % (behind_schedule*1000.0) self.last_print = now if self.last_time: delta = simulated_now - self.last_time else: delta = 0.0 self.last_time = simulated_now self.next_wakeup = None return delta
def nextDelta(self): now = time() if self.next_wakeup: simulated_now = self.next_wakeup else: simulated_now = now if now - self.last_print > 1.0: behind_schedule = now - simulated_now if behind_schedule > 0.1: print "Warning: running %.f ms behind schedule" % ( behind_schedule * 1000.0) self.last_print = now if self.last_time: delta = simulated_now - self.last_time else: delta = 0.0 self.last_time = simulated_now self.next_wakeup = None return delta
def schedule(self, f, wait_time): if self.scheduled_id: # if the following error occurs, it is probably due to a flaw in the logic of EventLoopControllerBase raise RuntimeException("EventLoop class intended to maintain at most 1 scheduled callback.") if wait_time == INFINITY: self.last_time = None self.next_wakeup = None is_scheduled = True else: now = time() if not self.last_time: self.last_time = now self.next_wakeup = self.last_time + wait_time # self.last_time is a very large value, and wait_time can be very small, so if self.next_wakeup - self.last_time < wait_time: # due to floating point imprecision, it is possible for a nonzero wait-time to advance simulated time not enough to pop the next event, potentially even causing the model to hang, so we always take the ceil of the exact result of the addition self.last_time + wait_time. self.next_wakeup = nextafter(self.next_wakeup, INFINITY) remaining = max(self.next_wakeup - now, 0.0) is_scheduled, self.scheduled_id = self.schedule_callback(f, remaining) return is_scheduled
def schedule(self, f, wait_time): if self.scheduled_id: # if the following error occurs, it is probably due to a flaw in the logic of EventLoopControllerBase raise RuntimeException( "EventLoop class intended to maintain at most 1 scheduled callback." ) if wait_time == INFINITY: self.last_time = None self.next_wakeup = None is_scheduled = True else: now = time() if not self.last_time: self.last_time = now self.next_wakeup = self.last_time + wait_time # self.last_time is a very large value, and wait_time can be very small, so if self.next_wakeup - self.last_time < wait_time: # due to floating point imprecision, it is possible for a nonzero wait-time to advance simulated time not enough to pop the next event, potentially even causing the model to hang, so we always take the ceil of the exact result of the addition self.last_time + wait_time. self.next_wakeup = nextafter(self.next_wakeup, INFINITY) remaining = max(self.next_wakeup - now, 0.0) is_scheduled, self.scheduled_id = self.schedule_callback( f, remaining) return is_scheduled
def elapsed(self): if self.last_time: return time() - self.last_time else: return 0.0