在2.4中杀死Windows异步Popen进程
我有一个测试脚本,需要打开一个进程(Pyro 服务器),执行一些调用打开的进程以获取信息的操作,当这一切完成后,需要关闭该进程。这都是临时服务器上自动化测试的一部分。
在 python 2.6 中,你可以这样做:
pyro_server = subprocess.Popen(['python', 'pyro_server.py'])
# Do stuff, making remote calls to the Pyro server on occasion
pyro_server.terminate()
唉,我在工作中被锁定在 python 2.4 中,所以我无法访问该函数。如果我让脚本结束,服务器当然会继续存在。在脚本退出之前我应该做什么来关闭/终止该进程?
I have a testing script that needs to open a process (a Pyro server), do some stuff that will call the opened process for information, and when it's all done will need to close the process back down. It's all part of an automated test on a staging server.
In python 2.6 you can do this:
pyro_server = subprocess.Popen(['python', 'pyro_server.py'])
# Do stuff, making remote calls to the Pyro server on occasion
pyro_server.terminate()
Alas, I'm locked into python 2.4 here at work so I don't have access to that function. And if I just let the script end of course the server lives on. What should I be doing to close/kill that process before the script exits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑将 subprocess.py 复制到 python2.4 dist-packages 目录。它应该可以正常工作,因为它是旧 popen 库的简单包装器。
Popen 对象终止函数仅执行以下操作:
导入操作系统、信号
os.kill(pid, signal.SIGKILL)
pid 是子进程的进程 id 。 signal.SIGKILL 是数字 9,是标准的 UNIX 终止信号。您可以在 此处了解如何在 python 2.4 中使用 popen 模块生成子进程并获取其 pid一个>:
Consider copying subprocess.py to your python2.4 dist-packages directory. It should just work as it's a simple wrapper around the old popen library.
The Popen object terminate function does nothing more than the following:
import os, signal
os.kill(pid, signal.SIGKILL)
pid is the child process's process id. signal.SIGKILL is the number 9, and is a standard unix kill signal. You can see how to spawn a subprocess and get its pid in python 2.4 with the popen module here:
@BrainCore:请注意,os.kill 在 python24 中的 Windows 上不可用,请检查文档。
我在使用 python24 时在 Windows 上杀死 subprocess.Popen 对象的解决方案是这样的:
@BrainCore: Note that os.kill is not available on windows in python24, check the docs.
My solution for killing a subprocess.Popen object on windows when using python24 is this: