def __init__(self, pausable=False, cancellable=False, maintenance_mode=HC.MAINTENANCE_FORCED, only_start_if_unbusy=False, stop_time=None, cancel_on_shutdown=True): self._key = HydrusData.GenerateKey() self._creation_time = HydrusData.GetNowFloat() self._pausable = pausable self._cancellable = cancellable self._maintenance_mode = maintenance_mode self._only_start_if_unbusy = only_start_if_unbusy self._stop_time = stop_time self._cancel_on_shutdown = cancel_on_shutdown self._start_time = HydrusData.GetNow() self._deleted = threading.Event() self._deletion_time = None self._begun = threading.Event() self._done = threading.Event() self._cancelled = threading.Event() self._paused = threading.Event() self._ui_update_pause_period = 0.1 self._next_ui_update_pause = HydrusData.GetNowFloat( ) + self._ui_update_pause_period self._yield_pause_period = 10 self._next_yield_pause = HydrusData.GetNow() + self._yield_pause_period self._bigger_pause_period = 100 self._next_bigger_pause = HydrusData.GetNow( ) + self._bigger_pause_period self._longer_pause_period = 1000 self._next_longer_pause = HydrusData.GetNow( ) + self._longer_pause_period self._exception = None self._urls = [] self._variable_lock = threading.Lock() self._variables = dict()
def Work(self): SchedulableJob.Work(self) if not self._stop_repeating.is_set(): self._next_work_time = HydrusData.GetNowFloat() + self._period self._scheduler.AddJob(self)
def Wake(self, next_work_time=None): if next_work_time is None: next_work_time = HydrusData.GetNowFloat() self._next_work_time = next_work_time self._scheduler.WorkTimesHaveChanged()
def DeleteVariable( self, name ): with self._variable_lock: if name in self._variables: del self._variables[ name ] if HydrusData.TimeHasPassedFloat( self._next_ui_update_pause ): time.sleep( 0.00001 ) self._next_ui_update_pause = HydrusData.GetNowFloat() + self._ui_update_pause_period
def GetDueString(self): due_delta = self._next_work_time - HydrusData.GetNowFloat() due_string = HydrusData.TimeDeltaToPrettyTimeDelta(due_delta) if due_delta < 0: due_string = 'was due {} ago'.format(due_string) else: due_string = 'due in {}'.format(due_string) return due_string
def SlotOK(self): if self._thread_slot_type is not None: if HG.controller.AcquireThreadSlot(self._thread_slot_type): return True else: self._next_work_time = HydrusData.GetNowFloat( ) + 10 + random.random() return False return True
def __init__(self, controller, scheduler, initial_delay, work_callable): self._controller = controller self._scheduler = scheduler self._work_callable = work_callable self._should_delay_on_wakeup = False self._next_work_time = HydrusData.GetNowFloat() + initial_delay self._thread_slot_type = None self._work_lock = threading.Lock() self._currently_working = threading.Event() self._is_cancelled = threading.Event()
def test_sleep(self): job = self._GetJob() self.assertFalse(job.IsAsleep()) job.Sleep(3) self.assertTrue(job.IsAsleep()) five_secs_from_now = HydrusData.GetNowFloat() + 5 with patch.object(HydrusData, 'GetNowFloat', return_value=five_secs_from_now): self.assertFalse(job.IsAsleep())
def Delay(self, delay): self._next_work_time = HydrusData.GetNowFloat() + delay self._scheduler.WorkTimesHaveChanged()
def __repr__(self): return repr(self.__class__) + ': ' + repr( self._work_callable ) + ' next in ' + HydrusData.TimeDeltaToPrettyTimeDelta( self._next_work_time - HydrusData.GetNowFloat())