Ric*_*ard 2 python windows-services
下面是我正在运行的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)
Run Code Online (Sandbox Code Playgroud)
编辑:
我试着尝试:除了自我.但结果仍然是相同的.当它崩溃时服务没有重新开始......请有任何想法吗?如果一个服务在崩溃的情况下无法重新启动,那么这个服务并不是那么有用......不妨将它作为.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
Run Code Online (Sandbox Code Playgroud)
下面是一个服务,它除以零以引发错误.如果出现错误,将发送一个事件并使用该服务退出os._exit(-1).该值必须是0以外的任何值,以便Windows知道该服务没有很好地退出.
from win32api import CloseHandle, GetLastError, SetConsoleCtrlHandler
import os
import sys
import time
import win32serviceutil
import win32service
import win32event
import servicemanager
import traceback
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "test"
_svc_display_name_ = "test"
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
try: # try main
self.main()
except:
servicemanager.LogErrorMsg(traceback.format_exc()) # if error print it to event log
os._exit(-1)#return some value other than 0 to os so that service knows to restart
def main(self):
while self.run == True:
time.sleep(30)
t = 1/0
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
Run Code Online (Sandbox Code Playgroud)