我用下面的命令启动一个子进程:

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)

然而,当我试图杀死使用:

p.terminate()

or

p.kill()

该命令一直在后台运行,因此我想知道如何实际终止该进程。

注意,当我使用以下命令运行时:

p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)

它在发出p.terminate()时成功终止。


当前回答

如果你可以使用psutil,那么这是完美的:

import subprocess

import psutil


def kill(proc_pid):
    process = psutil.Process(proc_pid)
    for proc in process.children(recursive=True):
        proc.kill()
    process.kill()


proc = subprocess.Popen(["infinite_app", "param"], shell=True)
try:
    proc.wait(timeout=3)
except subprocess.TimeoutExpired:
    kill(proc.pid)

其他回答

我可以用

from subprocess import Popen

process = Popen(command, shell=True)
Popen("TASKKILL /F /PID {pid} /T".format(pid=process.pid))

它杀死了cmd.exe和我命令的程序。

(在Windows上)

如果你可以使用psutil,那么这是完美的:

import subprocess

import psutil


def kill(proc_pid):
    process = psutil.Process(proc_pid)
    for proc in process.children(recursive=True):
        proc.kill()
    process.kill()


proc = subprocess.Popen(["infinite_app", "param"], shell=True)
try:
    proc.wait(timeout=3)
except subprocess.TimeoutExpired:
    kill(proc.pid)

完整的解决方案,将通过回调函数在达到超时或特定条件时终止运行进程(包括子树)。 在windows和Linux上都可以使用,从Python 2.7到本文撰写时的3.10。

使用pip Install command_runner安装

超时示例:

from command_runner import command_runner

# Kills ping after 2 seconds
exit_code, output = command_runner('ping 127.0.0.1', shell=True, timeout=2)

具体情况示例: 在这里,如果当前系统时间秒数为> 5,我们将停止ping

from time import time
from command_runner import command_runner

def my_condition():
    # Arbitrary condition for demo
    return True if int(str(int(time()))[-1]) > 5

# Calls my_condition() every second (check_interval) and kills ping if my_condition() returns True
exit_code, output = command_runner('ping 127.0.0.1', shell=True, stop_on=my_condition, check_interval=1)

这些答案都没有为我工作,所以我离开了工作的代码。在我的例子中,即使在用.kill()杀死进程并获得.poll()返回代码之后,进程也没有终止。

遵循子流程。Popen文档:

"...为了正确地清理,一个行为良好的应用程序应该杀死子进程并完成通信……”

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

在我的情况下,我在调用proc.kill()后错过了proc. communication()。这将清除进程stdin, stdout…并且终止了这个过程。

Python 3.5或以上有一个非常简单的方法(实际上在Python 3.8上测试)

import subprocess, signal, time
p = subprocess.Popen(['cmd'], shell=True)
time.sleep(5) #Wait 5 secs before killing
p.send_signal(signal.CTRL_C_EVENT)

然后,如果你有键盘输入检测或类似的东西,你的代码可能会在某个时候崩溃。在这种情况下,在给出错误的代码/函数行中,只需使用:

try:
    FailingCode #here goes the code which is raising KeyboardInterrupt
except KeyboardInterrupt:
    pass

这段代码所做的只是向正在运行的进程发送一个“CTRL+C”信号,这将导致进程被杀死。