Deploy Flask app as windows service

后端 未结 3 665
臣服心动
臣服心动 2020-12-28 09:30

I\'m using the template found here: Is it possible to run a Python script as a service in Windows? If possible, how?

Here\'s my run.py, which i\'ve installed as a se

3条回答
  •  抹茶落季
    2020-12-28 09:54

    I appended my code in SvcStop() last line. "self.ReportServiceStatus(win32service.SERVICE_STOPPED)"

    In my case, It's works for stopping service.

    from app import app
    
    import win32serviceutil
    import win32service
    import win32event
    import servicemanager
    import socket
    
    
    class AppServerSvc (win32serviceutil.ServiceFramework):
        _svc_name_ = "Flask App"
        _svc_display_name_ = "Flask App"
    
        def __init__(self,args):
            win32serviceutil.ServiceFramework.__init__(self,args)
            self.hWaitStop = win32event.CreateEvent(None,0,0,None)
            socket.setdefaulttimeout(60)
    
        def SvcStop(self):
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            win32event.SetEvent(self.hWaitStop)
            # !important! to report "SERVICE_STOPPED"
            self.ReportServiceStatus(win32service.SERVICE_STOPPED)
    
        def SvcDoRun(self):
            servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                                  servicemanager.PYS_SERVICE_STARTED,
                                  (self._svc_name_,''))
            self.main()
    
        def main(self):
            app.run(host = '192.168.1.6')
    
    if __name__ == '__main__':
        win32serviceutil.HandleCommandLine(AppServerSvc)
    

提交回复
热议问题