将Flask应用程序部署为Windows服务 [英] Deploy Flask app as windows service

查看:1019
本文介绍了将Flask应用程序部署为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这里找到的模板:是否有可能在Windows中运行Python脚本作为服务?如果可能的话,怎么样?



这里是我的run.py,我已经按照上面的链接中的说明安装了服务。

  from app import app 

import win32serviceutil
import win32service
import win32event
import servicemanager
导入套接字

$ b class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ =Flask App
_svc_display_name_ =Flask应用
$ b $ def __init __(self,args):
win32serviceutil.ServiceFramework .__ init __(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,无)
socket.setdefaulttimeout(60)

SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
$ b $ 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)

本地计算机上的Flask App服务启动,然后停止。
有些服务如果没有被其他服务或程序使用,就会自动停止。



任何想法我做错了吗?帐户 - 我不认为这是一个权限问题。

谢谢!

解决方案

我不能在 request 之外的Flask中访问 WSGIRequestHandler ,所以我使用进程

  import win32serviceutil 
import win32service
import win32event
从多进程导入进程servicemanager
导入进程





$ b $ TestService
_svc_display_name_ =Test Service
_svc_description_ =通过在命名管道上接收和回显消息来测试Python服务框架
$ b $ 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)


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 service following the instructions in the above link.

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 the message:

"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."

Any idea what I'm doing wrong? I have tried various user accounts--i don't think it's a permission problem.

Thanks!

解决方案

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)

这篇关于将Flask应用程序部署为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆