How can I close a flash application that runs as a service?

I managed to run my flash application, which was executed thanks to Is it possible to run a Python script as a service on Windows? If possible, how? but when it comes to stopping, I can't. I have to complete the process in task manager.

Here is my run.py, which I turn into a service through 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 material from this post: http://librelist.com/browser/flask/2011/1/10/start-stop-flask/#a235e60dcaebaa1e134271e029f801fe , but, unfortunately, it does not work either.

, 'server' . , , .

+4
3

- Werkzeug , Win32. :

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

Flask, , POST /shutdown. urllib2 . .

, , Win32.

, .

+14

You can also fool Flask by believing that you pressed Ctrl+ C:

def shutdown_flask(self):
    from win32api import GenerateConsoleCtrlEvent
    CTRL_C_EVENT = 0
    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)

Then just call shutdown_flask()in your SvcStop():

try:
    # try to exit gracefully
    self.shutdown_flask()
except Exception as e:
    # force quit
    os._exit(0)

In the event of a failure, shutdown_flask()for any reason os._exit(), it ensures that your service terminates (albeit with an unpleasant warning) by stopping the interpreter.

0
source

Source: https://habr.com/ru/post/1539833/


All Articles