我們首先來看下全部代碼:
# -*- coding: cp936 -*- import win32serviceutil import win32service import win32event class test1(win32serviceutil.ServiceFramework): _svc_name_ = "test_python" _svc_display_name_ = "test_python" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): # 先告訴SCM停止這個過程 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) # 設置事件 win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): # 等待服務被停止 win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) if __name__=='__main__': win32serviceutil.HandleCommandLine(test1)
這里注意,如果你需要更改文件名,比如將win32serviceutil.HandleCommandLine(test1)中的test1更改為你的文件名,同時class也需要和你的文件名一致,否則會出現服務不能啟動的問題。
新聞熱點
疑難解答