我有一个正在尝试编写的 Windows 系统服务。我正在尝试 POS 机的接口,所以理想情况下我想将此代码包含在系统服务中。然而,一些实验让我相信 Windows 系统服务只会执行基本任务而不是其他迭代。
我有另一个函数,我需要每隔 x 秒调用一次,这个附加函数是一个 while 循环,但我无法让我的函数和 win32 循环等待系统调用一起很好地发挥作用。我将在下面的代码中更详细地介绍。
import win32service
import win32serviceutil
import win32event
class PySvc(win32serviceutil.ServiceFramework):
# net name
_svc_name_ = "test"
_svc_display_name_ = "test"
_svc_description_ = "Protects your computer."
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self,args)
# create an event to listen for stop requests on
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
# core logic of the service
def SvcDoRun(self):
# if the stop event hasn't been fired keep looping
while rc != win32event.WAIT_OBJECT_0:
# block for 60 seconds and listen for a stop event
rc = win32event.WaitForSingleObject(self.hWaitStop, 60000)
## I want to put an additional function that uses a while loop here.
## The service will not work correctly with additional iterations, inside or
## the above api calls.
## Due to the nature of the service and the api call above,
## this leads me to have to compile an additional .exe and somehow call that
## from the service.
# called when we're being shut down
def SvcStop(self):
# tell the SCM we're shutting down
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# fire the stop event
win32event.SetEvent(self.hWaitStop)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PySvc)
我的研究表明,我需要以某种方式从 Windows 系统服务调用 .exe。有谁知道如何做到这一点?我尝试过使用 os.system,并且子进程模块的变体调用无济于事,似乎 windows 只是忽略了它们。有任何想法吗?
编辑:恢复到原来的问题