Пример #1
0
    def __init__(self,
                 command,
                 delay_provider=None,
                 initial_delay=datetime.timedelta(),
                 thread_name=None,
                 args=None,
                 kwargs=None):
        """
        Creates and executes a periodic action that will be run first without any delay and subsequently with the
        given delay between the termination of one execution and the commencement of the next.
        Scheduled thread gets terminated if exception thrown, hence subsequent execution will not happen.
        The scheduler can be paused, it will then keep running but not execute the command until resume() is called.

        :param command: a new thread will be spawned for executing this command with arguments
            specified in args and kwargs
        :param delay_provider: function providing the delay between executions as a timedelta, default returns always 1s
        :param initial_delay: delay before first execution as a timedelta, default is 0s.
        :param thread_name: name of the new spawned thread
        :param args: (list) passing argument by its position
        :param kwargs: (dict) passing argument by the arguments' names
        """
        self._command = command
        self._thread = \
            threading.Thread(target=self._schedule_task_execution, name=thread_name)
        self._thread.daemon = True
        self._args = args if args is not None else []
        self._kwargs = kwargs if kwargs is not None else {}
        self._state = ExecutionState(
            delay_provider=delay_provider if delay_provider else lambda: datetime.timedelta(seconds=1),
            initial_delay=initial_delay)
 def before(self):
     self.initial_delay = datetime.timedelta(seconds=10)
     self.default_delay = datetime.timedelta(seconds=20)
     self.first_pause_time = 3
     self.first_resume_time = 8
     self.mock_queue = MagicMock(name="queue", spec=Queue)
     self.mock_clock = MagicMock(name="clock", spec=time.time)
     self.mock_clock.side_effect = [
         0, self.first_pause_time, self.first_resume_time
     ]
     self.state = ExecutionState(delay_provider=lambda: self.default_delay,
                                 initial_delay=self.initial_delay,
                                 state_changes_queue=self.mock_queue,
                                 clock=self.mock_clock)
     # mock queue so that we simulate a pause command at 3 o'clock
     # then a resume command at 8 o'clock and then no change.
     # With initial delay being 10, we should wait for 7 more after resume.
     self.mock_queue.get.side_effect = \
         [
             {
                 "state": ExecutionState.PAUSED,
                 "current_time": self.first_pause_time
             },
             {
                 "state": ExecutionState.RUNNING,
                 "current_time": self.first_resume_time
             },
             Empty
         ]
 def before(self):
     self.initial_delay = datetime.timedelta(seconds=1)
     self.default_delay = datetime.timedelta(seconds=2)
     self.mock_queue = MagicMock(name="queue", spec=Queue)
     self.state = ExecutionState(delay_provider=lambda: self.default_delay,
                                 initial_delay=self.initial_delay,
                                 state_changes_queue=self.mock_queue,
                                 clock=lambda: 1)
     # mock queue so that we instantly return Empty without waiting for timeout
     self.mock_queue.get.side_effect = Empty
 def before(self):
     self.state = ExecutionState()
     self.executor = ExecutorForTest(self.state)
 def before(self):
     self.state = ExecutionState()
     self.executor = ExecutorForTest(self.state)
     yield
     self.state.signal_stop()