class Windows_Python_Service(win32serviceutil.ServiceFramework):
    _svc_name_ = "Windows_Python_Service"
    _svc_display_name_ = "Windows Python Service"
    _svc_description_ = "Windows Python Service" 
  
    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.run = True
    
    def SvcDoRun(self):
        while self.run:
            #服务逻辑
            print(time.time())
            #跳出服务结束
            time.sleep(5)
        
    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self.run = False
    
if __name__ == '__main__':
    try:
        if len(sys.argv) == 1:
            evtsrc_dll = os.path.abspath(servicemanager.__file__)
            servicemanager.PrepareToHostSingle(Windows_Python_Service)
            servicemanager.Initialize('Windows_Python_Service', evtsrc_dll)
            servicemanager.StartServiceCtrlDispatcher()
        else:
            win32serviceutil.HandleCommandLine(Windows_Python_Service)
    except:
        print('Please use the command:')
        print('Windows_Python_Service install')
        print('Windows_Python_Service start')
        print('Windows_Python_Service stop')
        print('Windows_Python_Service remove')
如果觉得我的文章对你有用,请随意赞赏