def start(self, interval, now=True): """Start running pre-set function every interval seconds. """ if interval < 0: raise ValueError('interval must be >= 0') if self._running: self.stop() self._running = True self._interval = interval if now: self._self_thread = hub.spawn_after(0, self) else: self._self_thread = hub.spawn_after(self._interval, self)
def _spawn_activity_after(self, seconds, activity, *args, **kwargs): self._validate_activity(activity) # Schedule to spawn a new greenthread after requested delay greenthread = hub.spawn_after(seconds, activity.start, *args, **kwargs) self._child_thread_map[activity.name] = greenthread self._child_activity_map[activity.name] = activity return greenthread
def reset(self): """Skip the next iteration and reset timer. """ if self._self_thread is not None: # Cancel currently scheduled call self._self_thread.cancel() self._self_thread = None # Schedule a new call self._self_thread = hub.spawn_after(self._interval, self)
def reset_neighbor(ip_address): neighs_conf = CORE_MANAGER.neighbors_conf neigh_conf = neighs_conf.get_neighbor_conf(ip_address) # Check if we have neighbor with given IP. if not neigh_conf: raise RuntimeConfigError('No neighbor configuration found for given' ' IP: %s' % ip_address) # If neighbor is enabled, we disable it. if neigh_conf.enabled: # Disable neighbor to close existing session. neigh_conf.enabled = False # Enable neighbor after NEIGHBOR_RESET_WAIT_TIME # this API works asynchronously # it's recommended to check it really reset neighbor later def up(): neigh_conf.enabled = True hub.spawn_after(NEIGHBOR_RESET_WAIT_TIME, up) else: raise RuntimeConfigError('Neighbor %s is not enabled, hence cannot' ' reset.' % ip_address) return True
def __call__(self): if self._running: # Schedule next iteration of the call. self._self_thread = hub.spawn_after(self._interval, self) self._funct(*self._args, **self._kwargs)
def _spawn_after(self, name, seconds, callable_, *args, **kwargs): self._validate_callable(callable_) greenthread = hub.spawn_after(seconds, callable_, *args, **kwargs) self._child_thread_map[name] = greenthread return greenthread