Python 杀死正在运行的子进程调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16866602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:52:05  来源:igfitidea点击:

Kill a running subprocess call

pythonmultithreadingsubprocess

提问by Thomas O

I'm launching a program with subprocesson Python.

我正在用subprocessPython启动一个程序。

In some cases the program may freeze. This is out of my control. The only thing I can do from the command line it is launched from is CtrlEscwhich kills the program quickly.

在某些情况下,程序可能会冻结。这是我无法控制的。我从它启动的命令行中唯一可以做的就是CtrlEsc快速杀死程序。

Is there any way to emulate this with subprocess? I am using subprocess.Popen(cmd, shell=True)to launch the program.

有什么办法可以模拟这个subprocess吗?我正在使用subprocess.Popen(cmd, shell=True)来启动该程序。

采纳答案by butch

p = subprocess.Popen("echo 'foo' && sleep 60 && echo 'bar'", shell=True)
p.kill()

Check out the docs on the subprocessmodule for more info: http://docs.python.org/2/library/subprocess.html

查看subprocess模块上的文档以获取更多信息:http: //docs.python.org/2/library/subprocess.html

回答by Aya

Well, there are a couple of methods on the object returned by subprocess.Popen()which may be of use: Popen.terminate()and Popen.kill(), which send a SIGTERMand SIGKILLrespectively.

好吧,返回的对象有几个方法subprocess.Popen()可能有用:Popen.terminate()and Popen.kill(),它们分别发送SIGTERMSIGKILL

For example...

例如...

import subprocess
import time

process = subprocess.Popen(cmd, shell=True)
time.sleep(5)
process.terminate()

...would terminate the process after five seconds.

...将在五秒后终止该过程。

Or you can use os.kill()to send other signals, like SIGINTto simulate CTRL-C, with...

或者您可以使用os.kill()发送其他信号,例如SIGINT模拟 CTRL-C,使用...

import subprocess
import time
import os
import signal

process = subprocess.Popen(cmd, shell=True)
time.sleep(5)
os.kill(process.pid, signal.SIGINT)

回答by ScotchAndSoda

Your question is not too clear, but If I assume that you are about to launch a process wich goes to zombie and you want to be able to control that in some state of your script. If this in the case, I propose you the following:

您的问题不太清楚,但是如果我假设您即将启动一个进入僵尸状态的进程,并且您希望能够在脚本的某种状态下控制它。如果是这种情况,我建议您:

p = subprocess.Popen([cmd_list], shell=False)

This in not really recommanded to pass through the shell. I would suggest you ti use shell=False, this way you risk less an overflow.

这并不是真的建议通过外壳。我建议你使用 shell=False,这样你就可以减少溢出的风险。

# Get the process id & try to terminate it gracefuly
pid = p.pid
p.terminate()

# Check if the process has really terminated & force kill if not.
try:
    os.kill(pid, 0)
    p.kill()
    print "Forced kill"
except OSError, e:
    print "Terminated gracefully"

回答by Asif Hasnain

You can use two signals to kill a running subprocess call i.e., signal.SIGTERM and signal.SIGKILL; for example

您可以使用两个信号来终止正在运行的子进程调用,即signal.SIGTERM 和signal.SIGKILL;例如

import subprocess
import os
import signal
import time
..
process = subprocess.Popen(..)
..
# killing all processes in the group
os.killpg(process.pid, signal.SIGTERM)
time.sleep(2)
if process.poll() is None:  # Force kill if process is still alive
    time.sleep(3)
    os.killpg(process.pid, signal.SIGKILL)

回答by Micheal Bee

Try wrapping your subprocess.Popen call in a try except block. Depending on why your process is hanging, you may be able to cleanly exit. Here is a list of exceptions you can check for: Python 3 - Exceptions Handling

尝试将您的 subprocess.Popen 调用包装在 try except 块中。根据您的进程挂起的原因,您可能可以干净地退出。以下是您可以检查的异常列表: Python 3 - 异常处理