我试图将两个进程加载到win32serviceutil.ServiceFramework中,但是什么也没有发生。 如果添加run()语句,则只有第一个过程有效。
当两个进程直接加载到 main 中时,它们同时工作。
import win32serviceutil, win32service, win32event, servicemanager
import traceback, sys, os, time
from multiprocessing import Process
class Service(win32serviceutil.ServiceFramework):
_svc_name_ = 'serviceteste'
_svc_display_name_ = 'Service Teste'
_svc_description_ = 'Service Teste'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.isAlive = True
def SvcStop(self):
self.isAlive = False
self.ReportServiceStatus(win32service.SERVICE_CONTROL_STOP)
self.pr_srv_1.terminate()
self.pr_srv_2.terminate()
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self.timeout = 10000
while self.isAlive:
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
if rc == win32event.WAIT_OBJECT_0:
servicemanager.LogInfoMsg(self._svc_name_ + " - STOPPED!")
break
else:
try:
self.pr_srv_1 = Process(target=proces_1).start()
self.pr_srv_2 = Process(target=proces_2).start()
except Exception as e:
print traceback.format_exc()
def proces_1():
while True:
print('Process 1...')
time.sleep(3)
def proces_2():
while True:
print('Process 2...')
time.sleep(3)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(Service)
使用win32serviceutil.ServiceFramework仅允许一个进程吗?