def schedule_absolute( self, duetime: typing.AbsoluteTime, action: typing.ScheduledAction, state: Optional[typing.TState] = None) -> typing.Disposable: """Schedules an action to be executed at duetime. Args: duetime: Absolute time at which to execute the action. action: Action to be executed. state: [Optional] state to be given to the action function. Returns: The disposable object used to cancel the scheduled action (best effort). """ if self._is_disposed: raise DisposedException() dt = self.to_datetime(duetime) si: ScheduledItem[typing.TState] = ScheduledItem( self, state, action, dt) with self._condition: if dt <= self.now: self._ready_list.append(si) else: self._queue.enqueue(si) self._condition.notify() # signal that a new item is available self._ensure_thread() return Disposable(si.cancel)
def schedule(self, action, state=None): """Schedules an action to be executed.""" if self.is_disposed: raise DisposedException() si = ScheduledItem(self, state, action, None) with self.condition: self.ready_list.append(si) self.condition.notify() # signal that a new item is available self.ensure_thread() return Disposable(si.cancel)
def schedule_absolute(self, duetime, action, state=None): """Schedules an action to be executed at duetime.""" if self.is_disposed: raise DisposedException() dt = self.to_datetime(duetime) si = ScheduledItem(self, state, action, dt) with self.condition: if dt < self.now(): self.ready_list.append(si) else: self.queue.enqueue(si) self.condition.notify() # signal that a new item is available self.ensure_thread() return Disposable(si.cancel)
def schedule_periodic(self, period: typing.RelativeTime, action: typing.ScheduledPeriodicAction, state: Optional[typing.TState] = None ) -> typing.Disposable: """Schedules a periodic piece of work. Args: period: Period in seconds or timedelta for running the work periodically. action: Action to be executed. state: [Optional] Initial state passed to the action upon the first iteration. Returns: The disposable object used to cancel the scheduled recurring action (best effort). """ if self._is_disposed: raise DisposedException() disposed: bool = False def invoke_periodic(scheduler, _): if disposed: return if period: scheduler.schedule_relative(period, invoke_periodic) nonlocal state new_state = action(state) if new_state is not None: state = new_state self.schedule_relative(period, invoke_periodic) def dispose(): nonlocal disposed disposed = True return Disposable(dispose)
def schedule_periodic( self, period: typing.RelativeTime, action: typing.ScheduledPeriodicAction, state: Optional[typing.TState] = None) -> typing.Disposable: """Schedules a periodic piece of work. Args: period: Period in seconds or timedelta for running the work periodically. action: Action to be executed. state: [Optional] Initial state passed to the action upon the first iteration. Returns: The disposable object used to cancel the scheduled recurring action (best effort). """ if self._is_disposed: raise DisposedException() return super().schedule_periodic(period, action, state=state)