如何处理在Python中编写的Windows服务信号?

我正在写一个Windows服务在Python中。 我已经把信号处理代码放在服务的SvcDoRun()方法中。 当我启动服务时,在日志文件中出现以下exception:exception:信号仅在主线程中起作用

以下是我的代码:

import pythoncom import win32serviceutil import win32service import win32event import socket import sys sys.path.append("E:\\AnuntaTech\\SecondDrop") from src.CPE.cpe import Cpe_MBus from src.Utilities import atlogger import threading import os import signal ''' This is Thread subclass to wait on the MessageBus Channel for CPE ''' class CpeThread(threading.Thread): def __init__(self,oCpeMbus): threading.Thread.__init__(self) self.oCpeMbus = oCpeMbus def run(self): self.oCpeMbus.mod.wait() ''' this function is the signal handler to re-register on the crash of MessageBus ''' def reRegisterSignalHandler(signum,frame): atlogger.info("in CpeMain : reRegisterSignalHandler()") atlogger.info("Re-registering CPE on MessageBus") oCpeMbus = Cpe_MBus() oCpeThread = CpeThread(oCpeMbus) oCpeThread.start() ''' this function is the signal handler to reboot CPE ''' def reBootSignalHandler(signum,frame): atlogger.info("In executorMain : reBootSignalHandler") # TODO: add-clean-up code os.kill(int(os.getpid()),signal.SIGKILL) class AppServerSvc (win32serviceutil.ServiceFramework): _svc_name_ = "MyService" _svc_display_name_ = "My Service" def __init__(self,args): try : win32serviceutil.ServiceFramework.__init__(self,args) self.hWaitStop = win32event.CreateEvent(None,0,0,None) socket.setdefaulttimeout(60) except Exception as e: atlogger.info("Exception: %s",e) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) self.fStop = True return True #pass #self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): import servicemanager servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_,'')) self.fStop = False self.main() def main(self): try : f = open('E:\\test.txt','w') f.write("in cpeMain") f.close() try : signal.signal(signal.SIGILL,reRegisterSignalHandler) # registering the signal handler signal.signal(signal.SIGTERM,reBootSignalHandler) # registering the signal handler except Exception as e : atlogger.info("Exception: %s",e) atlogger.info("This is CPE on Message Bus") #starting the CPE module oCpeMbus = Cpe_MBus() oCpeThread = CpeThread(oCpeMbus) oCpeThread.start() atlogger.info("CPE regitration done on MessageBus") except Exception as e: atlogger.info("Exception: %s",e) while True: pass if __name__ == '__main__': win32serviceutil.HandleCommandLine(AppServerSvc)