gpt4 book ai didi

python - 如何停止作为 Windows 服务运行的 web.py 服务器

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:30 24 4
gpt4 key购买 nike

import win32serviceutil
import win32service
import win32event
import thread
import json
import sys
import web
import os
import logging
from ReleaseDistribution.server_actions import Actions
from ReleaseDistribution import logging_setup
import time
from win32api import SetConsoleCtrlHandler
import traceback

class TestHttpService(win32serviceutil.ServiceFramework):
_svc_name_ = "GlobalDeploymentReleaseService"
_svc_display_name_ = "Global Deployment Release Service"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
SetConsoleCtrlHandler(lambda x: True, True)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.run_service=False

def SvcDoRun(self):

try:
app = web.application(urls, globals())
http_server = web.httpserver.runsimple(app.wsgifunc(), ('0.0.0.0', 8088))
http_server.run()
# raise any exceptions, we can't usually recover well
except Exception as error:
logger.error('exception...{0}'.format(traceback.format_exc()))
sys.exit(-1)

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

我是 Python 和窗口服务 api 的新手。我需要将 python 脚本转换为 Windows 服务。我成功地迈出了第一步,但我找不到阻止它的方法。我收到“[SC] ControlService FAILED 1061:服务此时无法接受控制消息。”当

我没有无限循环来检查 self.run_service 标志并调用 sys.exit() 来终止服务。你们能帮我解决一下吗?

最佳答案

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
web.httpserver.server.interrupt = KeyboardInterrupt()

wsgiserver.init.py的源代码:start()方法轮询中断来判断是否停止服务器。

     while self.ready:
self.tick()
if self.interrupt:
while self.interrupt is True:
# Wait for self.stop() to complete. See _set_interrupt.
time.sleep(0.1)
if self.interrupt:
raise self.interrupt

关于python - 如何停止作为 Windows 服务运行的 web.py 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26408147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com