zoukankan      html  css  js  c++  java
  • 将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)
    

      

  • 相关阅读:
    CentOS7设置hostname、hosts、静态IP地址、关闭防火墙
    排序算法总结对比
    Java——HashMap使用Demo
    Java——HashMap底层源码分析
    Java——LinkedList使用Demo
    laravel框架之及時更改
    laravel框架之批刪&全選&全不選&反選
    laravel框架之即點即改
    laravel框架之修改
    laravel框架之增刪改查
  • 原文地址:https://www.cnblogs.com/luomingchuan/p/3776113.html
Copyright © 2011-2022 走看看