我设法在pylons女服务员wsgi服务器(如下)中运行了一个运行flask的pywin32 Windows服务的演示示例。一个侄女自给自足的解决方案就是这个主意。
[我花了数小时来审查和测试使女服务员干净出口的方法(例如this和this),但到目前为止,我能做的最好的事情就是自杀SIGINT,这使Windows抱怨“管道已经结束”。当通过服务控制面板停止时,但至少它停止了:-/我猜pywin32启动的pythonservice.exe不应终止,只是女服务员踩到了?
说实话,我仍然不确定这是否是关于女服务员,pywin32或仅仅是纯python的问题。我do
感觉答案就在我的眼前,但现在我已经完全迷住了。欢迎提出任何建议!
import os
import random
import signal
import socket
from flask import Flask, escape, request
import servicemanager
import win32event
import win32service
import win32serviceutil
from waitress import serve
app = Flask(__name__)
@app.route('/')
def hello():
random.seed()
x = random.randint(1, 1000000)
name = request.args.get("name", "World")
return 'Hello, %s! - %s - %s' % (escape(name), x, os.getpid())
# based on https://www.thepythoncorner.com/2018/08/how-to-create-a-windows-service-in-python/
class SMWinservice(win32serviceutil.ServiceFramework):
'''Base class to create winservice in Python'''
_svc_name_ = 'WaitressService'
_svc_display_name_ = 'Waitress server'
_svc_description_ = 'Python waitress WSGI service'
@classmethod
def parse_command_line(cls):
'''
ClassMethod to parse the command line
'''
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
'''
Constructor of the winservice
'''
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
'''
Called when the service is asked to stop
'''
self.stop()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, ''))
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
'''
Called when the service is asked to start
'''
self.start()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def start(self):
pass
def stop(self):
print 'sigint'
os.kill(os.getpid(), signal.SIGINT)
def main(self):
print 'serve'
serve(app, listen='*:5000')
if __name__ == '__main__':
SMWinservice.parse_command_line()
我设法在pylons女服务员wsgi服务器(如下)中运行了一个运行flask的pywin32 Windows服务的演示示例。一个侄女自给自足的解决方案是这个主意。.我花了...
我已经找到了使用似乎可行的子线程的解决方案。不太确定这是否可能会带来意想不到的后果。