title: Deploy Flask app as windows service
body: <p>I'm using the template found here: <a href="https://stackoverflow.com/questions/32404/is-it-possible-to-run-a-python-script-as-a-service-in-windows-if-possible-how">Is it possible to run a Python script as a service in Windows? If possible, how?</a></p>
  
  <p>Here's my run.py, which i've installed as a service following the instructions in the above link. </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>However, when I try to start the service i get the message:</p>
  
  <p>"The Flask App service on Local Computer started and then stopped. 
  Some services stop automatically if they are not in use by other services or programs."</p>
  
  <p>Any idea what I'm doing wrong? I have tried various user accounts--i don't think it's a permission problem. </p>
  
  <p>Thanks!</p>