Deploy Flask app as windows service

后端 未结 3 664
臣服心动
臣服心动 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 10:03

    I can't access WSGIRequestHandler in Flask outside request, so I use Process.

    import win32serviceutil
    import win32service
    import win32event
    import servicemanager
    from multiprocessing import Process
    
    from app import app
    
    
    class Service(win32serviceutil.ServiceFramework):
        _svc_name_ = "TestService"
        _svc_display_name_ = "Test Service"
        _svc_description_ = "Tests Python service framework by receiving and echoing messages over a named pipe"
    
        def __init__(self, *args):
            super().__init__(*args)
    
        def SvcStop(self):
            self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            self.process.terminate()
            self.ReportServiceStatus(win32service.SERVICE_STOPPED)
    
        def SvcDoRun(self):
            self.process = Process(target=self.main)
            self.process.start()
            self.process.run()
    
        def main(self):
            app.run()
    
    
    if __name__ == '__main__':
        win32serviceutil.HandleCommandLine(Service)
    

提交回复
热议问题