meimei10251314 发表于 2017-4-29 09:00:54

python 脚本作为Windows服务启动

摘自: python 脚本作为Windows服务启动


#-*-coding:cp936-*-
importwin32serviceutil
importwin32service
importwin32event

classtest1(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)

defSvcStop(self):
#先告诉SCM停止这个过程
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
#设置事件
win32event.SetEvent(self.hWaitStop)

defSvcDoRun(self):
#等待服务被停止
win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)

if__name__=='__main__':
win32serviceutil.HandleCommandLine(test1)

这里注意,如果你需要更改文件名,比如将win32serviceutil.HandleCommandLine(test1)中的test1更改为你的文件名,同时class也需要和你的文件名一致,否则会出现服务不能启动的问题。

页: [1]
查看完整版本: python 脚本作为Windows服务启动