Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    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 after which to execute the action.
            action: Action to be executed.
            state: [Optional] state to be given to the action function.
        """

        duetime = self.to_datetime(duetime)
        si: ScheduledItem[typing.TState] = ScheduledItem(
            self, state, action, duetime)

        with self._lock:
            self._queue.enqueue(si)

        return si.disposable
Beispiel #4
0
    def schedule_relative(self,
                          duetime: typing.RelativeTime,
                          action: typing.ScheduledAction,
                          state: typing.TState = None) -> typing.Disposable:
        """Schedules an action to be executed after duetime.

        Args:
            duetime: Relative time after which to execute the action.
            action: Action to be executed.

        Returns:
            The disposable object used to cancel the scheduled action
            (best effort).
        """

        dt = self.now + self.to_timedelta(duetime)
        si: ScheduledItem[typing.TState] = ScheduledItem(
            self, state, action, dt)

        self.queue.enqueue(si)

        return si.disposable