Deploy Flask as a Windows Service

I am using the template found here: Is it possible to run a Python script as a service on Windows? If possible, how?

Here is my run.py, which I installed as a service, following the instructions in the link above.

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

However, when I try to start the service, I get a message:

"The Flask App service on the local computer started and then stopped. Some services automatically stop if they are not used by other services or programs.

Any idea what I'm doing wrong? I tried various user accounts - I don't think this is a permission problem.

Thanks!

+7
python windows flask service
source share
3 answers

I cannot access the WSGIRequestHandler in Flask outside of 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) 
+6
source share

Figured out this - I left the debugging option in app.run (). Once I removed it, good to go!

While the service starts and works correctly (I can access my flash application from another computer on the network), it cannot stop. In the thread with the published template that I used, the author mentions something about setting a flag to stop the service correctly.

Does anyone know what he means by this and how to encode it in order to properly stop the service?

+2
source share

I added my code to the last line of SvcStop (). "Self.ReportServiceStatus (win32service.SERVICE_STOPPED)"

In my case, it works to stop the 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) 
-3
source share

All Articles