将 python 脚本作为服务运行时出现问题
我用过这个问题 学习使用 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)
I have used this question to learn install my python script as a service with ActivePython. I am able to install my script and remove it, as I would expect, but my problem is that once I start the script running, I then lose the ability to stop it or remove it. It just runs permanently and I cannot get rid of it. The answer to the linked question mentions checking a flag to stop the script. Can someone explain how to do this?
Right now the script does very little. just prints lines to a file every few seconds. I want to get it working before I move on to more complicated things.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然你无法阻止它,你陷入了无限循环。将: 替换
为:
应该可以解决问题。
Of course you can't stop it, you're stuck in an infinite loop. Replace:
with this:
Should do the trick.
“检查标志”意味着让脚本(在 while True 循环内)寻找某些东西,几乎任何东西,作为跳出循环的信号。它可以检查具有特定名称的文件是否存在,并在创建文件时退出。只要您可以手动或通过控制应用程序创建信号,该服务就可以查找相同的信号。
至于“无法阻止它”,你的意思是即使你尝试用Windows任务管理器杀死它,它仍然会继续运行?
"Checking a flag" means having the script (inside the while True loop) look for something, almost anything, as a signal to break out of the loop. It could check for the existence of a file with a particular name and exit when the file is created. As long as you can manually or through a control application create the signal, the service can look for that same signal.
As far as "not being able to stop it", do you mean that it keeps running even if you try to kill it with the Windows task manager?