码迷,mamicode.com
首页 > 编程语言 > 详细

将python的程序包装成windows下的service

时间:2014-06-08 19:27:50      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   tar   

  使用python编写的脚本应用程序,在运行的时候需要有python的运行环境,但是我们肯定是希望整个python程序能够像应用程序一样打包生成一个包括其运行环境的exe文件包,这是第一步,但是要想使用net start这样的方式启动, 还需要将该exe注册到服务里面去,使用exe install完成。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#SmallestService.py 
#AsampledemonstratingthesmallestpossibleservicewritteninPython. 
  
importwin32serviceutil 
importwin32service 
importwin32event 
  
classSmallestPythonService(win32serviceutil.ServiceFramework): 
    _svc_name_="SmallestPythonService"
    _svc_display_name_="ThesmallestpossiblePythonService"
    def__init__(self,args): 
        win32serviceutil.ServiceFramework.__init__(self,args) 
        #Createaneventwhichwewillusetowaiton. 
        #The"servicestop"requestwillsetthisevent. 
        self.hWaitStop=win32event.CreateEvent(None,0,0,None
  
  defSvcStop(self): 
     #Beforewedoanything,telltheSCMwearestartingthestopprocess. 
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
        #Andsetmyevent. 
        win32event.SetEvent(self.hWaitStop)
 
    defSvcDoRun(self):
        #把你的程序代码放到这里就OK了
        win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
 
if__name__==‘__main__‘:
    win32serviceutil.HandleCommandLine(SmallestPythonService)

  

将python的程序包装成windows下的service,布布扣,bubuko.com

将python的程序包装成windows下的service

标签:c   class   blog   code   a   tar   

原文地址:http://www.cnblogs.com/luomingchuan/p/3776113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!