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

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

 

#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)

  

转载于:https://www.cnblogs.com/luomingchuan/p/3776113.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值