在 Solaris 上使用 Python 终止进程时出现问题
我有一个名为 C 的 C++ 程序,该程序设计为在收到 SIGINT 信号时关闭。我编写了一个Python程序P,它作为子进程运行C。我希望P阻止C。我尝试了 3 件事,我想知道为什么其中一些不起作用。
尝试#1:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.killpg(p.pid, signal.SIGINT)
这段代码给了我错误
OSError [Errno 3]:没有这样的进程`
即使 p.pid
与 ps。
尝试#2:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.system('kill -SIGINT %u' % p.pid)
这给了我错误
sh:kill:信号不好`
即使 kill -SIGINT
从终端工作。
尝试#3:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.system('kill -2 %u' % p.pid)
这有效。
我的问题是,为什么 #1 和 #2 不起作用?
编辑:我最初的假设是,由于 os.kill()
的文档说 New in version 2.7: Windows support
,我认为 os.kill()
(a) 首先在 2.7 中可用,(b) 在 Windows 中运行。阅读下面的答案后,我在 Solaris 上运行了 os.kill() ,抱歉,我应该首先这样做,它在 2.4 中确实有效。显然,该文档意味着 Windows 支持是 2.7 中的新功能。哦。
I have a C++ program, called C, that is designed to shut down when it receives a SIGINT signal. I've written a Python program P that runs C as a subprocess. I want P to stop C. I tried 3 things and I'd like to know why some of them didn't work.
Attempt #1:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.killpg(p.pid, signal.SIGINT)
This code gives me the error
OSError [Errno 3]: No such process`
even though the p.pid
matches the pid
displayed by ps.
Attempt #2:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.system('kill -SIGINT %u' % p.pid)
This gives me the error
sh: kill: bad signal`
even though kill -SIGINT <pid>
works from the terminal.
Attempt #3:
import subprocess
import signal
import os
p = subprocess.Popen(...)
...
os.system('kill -2 %u' % p.pid)
This works.
My question is, why didn't #1 and #2 work?
Edit: my original assumption was that since the documentation for os.kill()
says New in version 2.7: Windows support
, I thought that os.kill()
is (a) first available in 2.7 and (b) works in Windows. After reading the answers below, I ran os.kill()
on Solaris, which I should have done in the first place sorry, and it does work in 2.4. Obviously, the documentation means that Windows support is new in 2.7. Opps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个失败是因为 os.killpg 杀死了由其领导者标识的进程组;您有一个简单的流程,而不是流程组。尝试使用 os.kill 来代替。第二个失败是因为 shell 内置
kill
可以理解符号信号,但 Solaris 上的外部命令不能(而在 *BSD 和 Linux 上可以);使用数字信号(SIGINT
在 Solaris 上为2
,或者使用signal
模块中的 Python 预定义信号常量)。也就是说,使用Popen
自己的界面,而不是像其他人提到的那样;不要重新发明轮子,你可能会创造一些角落。The first fails because
os.killpg
kills a process group, identified by its leader; you have a simple process, not a process group. Tryos.kill
instead. The second fails because the shell builtinkill
understands symbolic signals, but the external command on Solaris doesn't (whereas on *BSD and Linux it does); use a numeric signal (SIGINT
is2
on Solaris, or use Python's predefined signal constants from thesignal
module). That said, usePopen
's own interface instead as mentioned by someone else; don't reinvent the wheel, you're liable to create some corners.Popen
对象有一个kill()
方法,您可以调用该方法以及terminate()
方法和通用 < href="http://docs.python.org/library/subprocess.html#subprocess.Popen.send_signal" rel="nofollow">send_signal()
方法。我会使用其中之一,而不是尝试与
os
接口一起使用的任何带外内容。您已经掌握了该过程的句柄,您应该使用它!The
Popen
object has akill()
method that you can invoke as well as aterminate()
method and a genericsend_signal()
method.I would use one of these rather than trying any of the out of band stuff you'd use with the
os
interface. You've already got a handle to the process, you should use it!