我曾使用这个问题来学习如何使用ActivePython将我的Python脚本安装为服务。我能够像预期一样安装和卸载脚本,但是我的问题是一旦我开始运行脚本,我就失去了停止或移除它的能力。它会一直运行,我无法摆脱它。链接问题的答案提到检查一个标志以停止脚本。有人可以解释如何做到这一点吗?
现在脚本几乎什么也没做,只是每隔几秒钟将行打印到文件中。我想在继续进行更复杂的事情之前先让它正常工作。
现在脚本几乎什么也没做,只是每隔几秒钟将行打印到文件中。我想在继续进行更复杂的事情之前先让它正常工作。
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "PythonTest"
_svc_display_name_ = "Python Test"
_svc_description_ = "This is a test of installing Python services"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
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):
i=0
while True:
f=open("C:\\hello.txt","a")
f.write("hello"+str(i)+"\n")
f.close()
i=i+1
time.sleep(5)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)