Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

python - How do I terminate a flask app that's running as a service?

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.

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)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can stop the Werkzeug web server gracefully before you stop the Win32 server. Example:

from flask import request

def shutdown_server():
    func = request.environ.get('werkzeug.server.shutdown')
    if func is None:
        raise RuntimeError('Not running with the Werkzeug Server')
    func()

@app.route('/shutdown', methods=['POST'])
def shutdown():
    shutdown_server()
    return 'Server shutting down...'

If you add this to your Flask server you can then request a graceful server shutdown by sending a POST request to /shutdown. You can use requests or urllib2 to do this. Depending on your situation you may need to protect this route against unauthorized access.

Once the server has stopped I think you will have to no problem stopping the Win32 service.

Note that the shutdown code above appears in this Flask snippet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...