ewo*_*wok 3 python windows service activepython
我已经使用这个问题来学习使用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)
Run Code Online (Sandbox Code Playgroud)
当然你无法阻止它,你陷入无限循环.更换:
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)
有了这个:
if win32event.WaitForSingleObject(self.hWaitStop, 5000) == win32event.WAIT_OBJECT_0:
break
Run Code Online (Sandbox Code Playgroud)
应该做的伎俩.
| 归档时间: |
|
| 查看次数: |
2437 次 |
| 最近记录: |