将 Flask Web 应用程序作为服务运行
这是通用示例的变体。你只需要导入你的应用程序脚本并在服务的 main()
函数中调用它的 run()
方法。在这种情况下,由于访问 WSGIRequestHandler
的问题,我们也使用多处理模块。
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)