def SvcDoRun(self): # Log a "started" message to the event log. servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) ProcETL().SendSms('ProcJob开始运行') #如果系统日期不是2007年,将时间调整为2007 (y, m, d, h, mi, s) = time.localtime()[0:6] if y != 2007: setdate = '%04d-%02d-%02d' % (2007, m, d) os.system('date %s' % setdate) win32event.SetWaitableTimer(self.timerForProcess, 0, 1000 * 60 * 1, None, None, 0) win32event.SetWaitableTimer(self.timerForPrepare, 0, 1000 * 60 * 2, None, None, 0) while 1: timeout = win32event.INFINITE waitHandles = self.hWaitStop, self.timerForProcess, self.timerForPrepare rc = win32event.WaitForMultipleObjects(waitHandles, 0, timeout) if rc == win32event.WAIT_OBJECT_0: # Stop event break elif rc == win32event.WAIT_OBJECT_0 + 1: ProcETL().ETLProcess() elif rc == win32event.WAIT_OBJECT_0 + 2: ProcETL().ETLPrepare() win32event.CancelWaitableTimer(self.timerForProcess) win32event.CancelWaitableTimer(self.timerForPrepare) servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, ''))
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 start(self) -> None: self.stopped = False if self.thread is None or not self.thread.is_alive(): name = f"Cyclic send task for 0x{self.messages[0].arbitration_id:X}" self.thread = threading.Thread(target=self._run, name=name) self.thread.daemon = True if HAS_EVENTS: win32event.SetWaitableTimer(self.event.handle, 0, self.period_ms, None, None, False) self.thread.start()
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 set_timer(self, interval): global keepalive_interval win32event.SetWaitableTimer(self.handle, -10000000 * interval, 1000 * interval, 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)
def send_initial_restarter_signals(): win32event.SetEvent(terminate_event) win32event.SetWaitableTimer(restart_event, 0, 0, None, None, 0)
def my_win32_watcher(): CREATED = 1 DELETED = 2 UPDATED = 3 RENAMED_FROM = 4 RENAMED_TO = 5 ACTIONS = { 1: "Created", 2: "Deleted", 3: "Updated", 4: "Renamed from something", 5: "Renamed to something" } # Thanks to Claudio Grondi for the correct set of numbers FILE_LIST_DIRECTORY = 0x0001 path_to_watch = "." hDir = win32file.CreateFile( path_to_watch, FILE_LIST_DIRECTORY, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, None, win32con.OPEN_EXISTING, win32con.FILE_FLAG_BACKUP_SEMANTICS, None ) while 1: # # ReadDirectoryChangesW takes a previously-created # handle to a directory, a buffer size for results, # a flag to indicate whether to watch subtrees and # a filter of what changes to notify. # # NB Tim Juchcinski reports that he needed to up # the buffer size to be sure of picking up all # events when a large number of files were # deleted at once. # results = win32file.ReadDirectoryChangesW( hDir, 1024, True, win32con.FILE_NOTIFY_CHANGE_FILE_NAME | win32con.FILE_NOTIFY_CHANGE_DIR_NAME | win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | win32con.FILE_NOTIFY_CHANGE_SIZE | win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | win32con.FILE_NOTIFY_CHANGE_SECURITY, None, None ) do_reload = False for action, file_path in results: if file_path == ".reloadignore": logger.debug("reloading ignore config") reload_ignore_config() if file_triggers_reload(file_path): #l = file_path, file_triggers_reload(file_path) do_reload = True break if do_reload: # terminate on first file change win32event.SetEvent(terminate_event) # 50 ms rollup window for starting reloading win32event.CancelWaitableTimer(restart_event) win32event.SetWaitableTimer(restart_event, RESTART_EVENT_DT, 0, None, None, 0)
# 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!")