将服务用作具有多个线程的控制器。
一个线程(Main)应该同步和排队命令,并使用win32 serviceframework进行通信、注册到系统等。
另一个线程(Worker)应该等待来自队列的命令并执行它们。如果您将任意代码作为单独的进程执行,那么您可以从工作线程中派生这些代码,并在它们完成后简单地读回结果并进行清理。在
这样,当一个stop到达时,您的主线程将把它注册到它队列中的worker,它将唤醒并尝试向其他进程发出退出信号,稍等片刻,然后强制清理或终止它们。在
更新:
下面是一个示例概念,介绍如何使服务始终响应并根据需要运行多长时间。每个工人都可以扔...
import threading
...
class InterruptedException(Exception):
pass
class WorkerThread(threading.Thread):
def __init__(self, controller):
self._controller = controller
self._stop = threading.Event()
super(WorkerThread, self).__init__()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
try:
# Insert the code you want to run as a service here
# rather than do "execfile(.../.../blah)" simply do:
# You can have your code throw InterruptedException if your code needs to exit
# Also check often if self.stopped and then cleanly exit
import your_file
your_file.main()
# if code in another module is not yours or cannot check often if it should stop then use multiprocessing which will spawn separate processes that you can terminate then from here when you need to stop and return
# in that case simply block here on self._stop.wait()
except InterruptedException as exc:
# We are forcefully quitting
pass
except Exception as e:
# Oh oh, did not anticipate this, better report to Windows or log it
finally:
# Close/release any connections, handles, files etc.
# OK, we can stop now
win32event.SetEvent(self._controller)
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.hWaitDone = win32event.CreateEvent(None, 0, 0, None)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
worker = WorkerThread(self.hWaitDone)
worker.start()
while True:
# Wait for service stop signal
rc = win32event.WaitForMultipleObjects([self.hWaitStop, self.hWaitDone], win32event.INFINITE)
# Check to see if self.hWaitStop happened as part of Windows Service Management
if rc == 0:
# Stop signal encountered
servicemanager.LogInfoMsg(self._svc_name_ + " - STOPPED!") #For Event Log
break
if rc == 1:
# Wait until worker has fully finished
worker.join()
# Determine from worker state if we need to start again (because run finished)
# Or do whatever
if not worker.need_to_start_again():
break
worker.start()