灵舞

导航

python 脚本作为Windows服务启动(3)

使用python脚本也可以作为windows的服务程序运行
只要下载并安装pywin32模块,就可以很轻松的开发windows服务了
源码如下:

  1. import win32serviceutil
  2. import win32service
  3. import win32event
  4. class test1(win32serviceutil.ServiceFramework):
  5.     _svc_name_ = "python_service"
  6.     _svc_display_name_ = "python_service"
  7. def __init__(self,args):
  8.         win32serviceutil.ServiceFramework.__init__(self,args)
  9. self.hWaitStop = win32event.CreateEvent(None,0,0,None)
  10. pass
  11. def SvcStop(self):
  12. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  13.         win32event.SetEvent(self.hWaitStop)
  14. pass
  15. def SvcDoRun(self):
  16.         win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
  17. pass
  18. pass
  19. if __name__ == '__main__':
  20.     win32serviceutil.HandleCommandLine(test1)

posted on 2010-07-09 21:07  灵舞  阅读(642)  评论(0编辑  收藏  举报