def __init__( self, bus: "BusABC", lock: threading.Lock, messages: Union[Sequence[Message], Message], period: float, duration: Optional[float] = None, on_error: Optional[Callable[[Exception], bool]] = None, ): """Transmits `messages` with a `period` seconds for `duration` seconds on a `bus`. The `on_error` is called if any error happens on `bus` while sending `messages`. If `on_error` present, and returns ``False`` when invoked, thread is stopped immediately, otherwise, thread continuiously tries to send `messages` ignoring errors on a `bus`. Absence of `on_error` means that thread exits immediately on error. :param on_error: The callable that accepts an exception if any error happened on a `bus` while sending `messages`, it shall return either ``True`` or ``False`` depending on desired behaviour of `ThreadBasedCyclicSendTask`. """ super().__init__(messages, period, duration) self.bus = bus self.send_lock = lock self.stopped = True self.thread = None self.end_time = time.perf_counter() + duration if duration else None self.on_error = on_error if HAS_EVENTS: self.period_ms: int = int(round(period * 1000, 0)) self.event = win32event.CreateWaitableTimer(None, False, None)
def __init__(self, interval=None): self.handle = win32event.CreateWaitableTimer(None, 0, None) if interval: self.set_timer(interval) # initialize parent threading.Thread.__init__(self)
def testWaitableTrigger(self): h = win32event.CreateWaitableTimer(None, 0, None) # for the sake of this, pass a long that doesn't fit in an int. dt = -2000000000 win32event.SetWaitableTimer(h, dt, 0, None, None, 0) rc = win32event.WaitForSingleObject(h, 10) # 10 ms. self.failUnlessEqual(rc, win32event.WAIT_TIMEOUT)
def __target(timeout=60): if platform.uname()[0].lower() == "windows": import win32con import win32event self.running = True kill = win32event.CreateEvent(None, 1, 0, None) pulse = win32event.CreateWaitableTimer(None, 0, None) win32event.SetWaitableTimer(pulse, 0, timeout * 1000, None, None, False) while (self.running): try: result = win32event.WaitForMultipleObjects( [kill, pulse], False, 1000) # if kill signal received, break loop if (result == win32con.WAIT_OBJECT_0): break # elif timeout has passed, generate a pulse elif (result == win32con.WAIT_OBJECT_0 + 1): self['event']() except: self.notifyOfError( "Pacemaker shutdown. Heartbeats will not be generated." ) win32event.SetEvent(kill) elif self.options['Verbose']: print "Pacemaker only supported in Windows at this time. "
def run(self): ringTrack = form.comboBox.currentText() ringHour = int(form.timeEdit.dateTime().time().hour()) ringMin = int(form.timeEdit.dateTime().time().minute()) timerVal = (ringHour * 60 * 60 * 10000000 + ringMin * 60 * 10000000) h = win32event.CreateWaitableTimer(None, 1, None) form.pushButton.setEnabled(False) win32event.SetWaitableTimer(h, -timerVal, 0, None, None, 1) win32event.WaitForSingleObject(h, win32event.INFINITE) # backup: (1 * 60 * 10000000) os.startfile("mp3\\" + ringTrack) form.label_3.setText('') form.pushButton.setEnabled(True)
def __init__( self, bus: "BusABC", lock: threading.Lock, messages: Union[Sequence[Message], Message], period: float, duration: Optional[float] = None, ): super().__init__(messages, period, duration) self.bus = bus self.send_lock = lock self.stopped = True self.thread = None self.end_time = time.perf_counter() + duration if duration else None if HAS_EVENTS: self.period_ms: int = int(round(period * 1000, 0)) self.event = win32event.CreateWaitableTimer(None, False, None) self.start()
def testWaitableError(self): h = win32event.CreateWaitableTimer(None, 0, None) h.close() self.assertRaises(pywintypes.error, win32event.SetWaitableTimer, h, -42, 0, None, None, 0)
def testWaitableFire(self): h = win32event.CreateWaitableTimer(None, 0, None) dt = -160 # 160 ns. win32event.SetWaitableTimer(h, dt, 0, None, None, 0) rc = win32event.WaitForSingleObject(h, 1000) self.failUnlessEqual(rc, win32event.WAIT_OBJECT_0)
def testWaitableFireLong(self): h = win32event.CreateWaitableTimer(None, 0, None) dt = int2long(-160) # 160 ns. win32event.SetWaitableTimer(h, dt, 0, None, None, 0) rc = win32event.WaitForSingleObject(h, 1000) assert rc == win32event.WAIT_OBJECT_0
def testWaitableError(self): h = win32event.CreateWaitableTimer(None, 0, None) h.close() with pytest.raises(pywintypes.error): win32event.SetWaitableTimer(h, -42, 0, None, None, 0)
# # HAS TO BE called AFTER process has been terminated win32api.SetConsoleCtrlHandler(None, False) diff = default_timer() - start logger.debug("Termination took: %.4f" % diff) logging.debug("TERMINATE OVER") ### ### process_handler = None # type: ProcessHandler terminate_event = win32event.CreateEvent(None, 0, 0, None) restart_event = win32event.CreateWaitableTimer(None, 0, None) # RESTART_EVENT_DT = -1000 * 100 * 5 # 0.05s RESTART_EVENT_DT = 1 spec = None reload_lock = threading.Lock() DEFAULT_RELOADIGNORE = """ # Ignore everything .. * */ # .. except *.py files !*.py """
# Uses the Windows API to set a timer, triggering that section of the script at regular intervals. # Potentially useful when used alongside the contents of the ReadDirectoryChangesW example. # Used in the pdf-to-Airtable program to post to Slack once a day while still watching the folder and having a 20 minute timeout. import win32event, time timerHandle = win32event.CreateWaitableTimer(None, True, None) print(time.time()) while True: win32event.SetWaitableTimer( timerHandle, -10000000, 0, None, None, True) # sets of 100 nanoseconds. -10,000,000 = 1 second waitForTimer = win32event.WaitForSingleObject( timerHandle, 20000 ) # waits for either the timer to trigger, or the 20,000 millisecond (20 second) timeout, whichever comes first. if waitForTimer == win32event.WAIT_OBJECT_0: print(time.time()) print("Timer worked!") elif waitForTimer == win32event.WAIT_TIMEOUT: print("Timer did not trigger before the timeout!")
def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.timerForProcess = win32event.CreateWaitableTimer(None, 0, None) self.timerForPrepare = win32event.CreateWaitableTimer(None, 0, None) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)