如何终止作为服务运行的Flask应用程序? [英] How do I terminate a flask app that's running as a service?

查看:828
本文介绍了如何终止作为服务运行的Flask应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于

I was able to get my flask app running as a service thanks to Is it possible to run a Python script as a service in Windows? If possible, how?, but when it comes to stopping it i cannot. I have to terminate the process in task manager.

这是我的run.py,通过run.py install将其变成服务:

Here's my run.py which I turn into a service via run.py install:

from app import app

from multiprocessing import Process
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket


class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "CCApp"
    _svc_display_name_ = "CC 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)
        server.terminate()
        server.join()

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        server = Process(app.run(host = '192.168.1.6'))
        server.start()

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

我从此帖子中获得了流程内容: http://librelist.com/browser/flask/2011/1/10/start-stop-flask/#a235e60dcaebaa1e134271e029f801fe ,但不幸的是,它也不起作用.

I got the process stuff from this post: http://librelist.com/browser/flask/2011/1/10/start-stop-flask/#a235e60dcaebaa1e134271e029f801fe but unfortunately it doesn't work either.

事件查看器中的日志文件显示未定义全局变量服务器".但是,我已经将服务器设为全局变量,但它仍然给我同样的错误.

The log file in Event Viewer says that the global variable 'server' is not defined. However, i've made server a global variable and it still gives me the same error.

推荐答案

我建议您使用 http://supervisord.org/.实际上在Windows中不起作用,但是使用Cygwin,您可以像在Linux中一样运行超级用户,包括作为服务运行.

I recommend you use http://supervisord.org/. Actually not work in Windows, but with Cygwin you can run supervisor as in Linux, including run as service.

对于安装Supervisord: https://stackoverflow.com/a/18032347/3380763

For install Supervisord: https://stackoverflow.com/a/18032347/3380763

安装后,您必须配置该应用程序,此处为示例:

After install you must configure the app, here an example: http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/ (Is not necessary that you use Nginx with the Supervisor's configuration is enough)

这篇关于如何终止作为服务运行的Flask应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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