【发布时间】:2018-04-10 06:41:57
【问题描述】:
我正在尝试使用多个线程在 python 上创建 Windows 服务。
我编写了按我的需要工作的代码,但我不明白它是如何破坏线程的!
为什么break in while 循环会中断不在while 循环中的线程?
import win32serviceutil
import win32service
import win32event
import servicemanager
import threading
from classifier import clf, refit
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "MyService"
_svc_display_name_ = "My Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
t1 = threading.Thread(target=clf)
t2 = threading.Thread(target=refit)
t1.start()
t2.start()
while True:
rc = win32event.WaitForSingleObject(self.hWaitStop, 100)
if rc == win32event.WAIT_OBJECT_0:
break
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
【问题讨论】:
标签: python multithreading winapi service