下面是我正在运行的windows服务的框架。发生错误时,会记录错误并在事件查看器中查看。问题是,脚本只是退出,不会再次重新启动,即使我已将恢复设置为在第一次、第二次和后续失败时重新启动服务。目前我很少处理错误,因为我想知道事件查看器中可能出现的错误,以便我可以编写代码来相应地处理这些错误。from win32api import CloseHandle, GetLastError, SetConsoleCtrlHandler
import os
import sys
import time
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "my_service_name"
_svc_display_name_ = "my 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 = False
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.run = True
self.main()
def main(self):
while self.run == True
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
编辑:
我试着尝试:除了self.main,结果还是一样的。服务崩溃时没有重新启动。。。有人有什么想法吗?如果服务在崩溃时无法重新启动,那么它就不是那么有用。。。不如用.pyc运行它
编辑:
下面是我的脚本中可能出现的错误示例。。。我不认为此错误消息特别有用,因为我试图实现的是重新启动服务,但这里有一个错误示例,该错误在不重新启动服务的情况下使我的服务崩溃:The instance's SvcRun() method failed
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 806, in SvcRun
self.SvcDoRun()
File "C:\Some_Service.py", line 46, in SvcDoRun
self.main()
File "Some_Service.py", line 61, in main
ser = self.open_serial_port()
File "Some_Service.py", line 70, in open_serial_port
serial_connection.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 56, in open
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
SerialException: could not open port COM6: [Error 1225] The remote system refused the network connection.
%2: %3