服务可以安装,但是启动就自动停止,弹窗提示本地计算机上的HelloWorld Service启动后停止,某些服务在未由其他服务或程序使用时将自动停止
,通过run.py
运行没有问题,求解什么原因?
class HelloWorldSvc (win32serviceutil.ServiceFramework): _svc_name_ = "HelloWorld-Service" _svc_display_name_ = "HelloWorld Service" def __init__(self,args): win32serviceutil.ServiceFramework.__init__(self,args) self.stop_event = win32event.CreateEvent(None,0,0,None) socket.setdefaulttimeout(60) self.stop_requested = False def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.stop_event) logging.info('Stopping service ...') self.stop_requested = True def SvcDoRun(self): servicemanager.LogMsg( servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '') ) self.main() @staticmethod def main(): # 运行程序 file_path = os.path.split(os.path.realpath(__file__))[0] + '\\run.py' print file_path try: execfile(file_path) except: pass if __name__ == '__main__': win32serviceutil.HandleCommandLine(HelloWorldSvc)
要不要试试在SvcDoRun方法里写个while循环,每次sleep个1秒,循环60次结束看看是不是service启动后1分钟才停止? 试试看呢。