使用 KeyboardInterrupt 终止子进程

lim*_*i44 4 python windows subprocess interrupt terminate

我正在使用 Python 使用 subprocess 模块调用 C++ 程序。由于程序需要一些时间来运行,我希望能够使用 Ctrl+C 来终止它。我在 StackOverflow 上看到了一些关于此的问题,但似乎没有一个解决方案对我有用。

我想要的是在 KeyboardInterrupt 上终止子进程。这是我拥有的代码(类似于其他问题中的建议):

import subprocess

binary_path = '/path/to/binary'
args = 'arguments' # arbitrary

call_str = '{} {}'.format(binary_path, args)

proc = subprocess.Popen(call_str)

try:
    proc.wait()
except KeyboardInterrupt:
    proc.terminate()
Run Code Online (Sandbox Code Playgroud)

但是,如果我运行它,代码会挂起,等待进程结束并且永远不会注册 KeyboardInterrupt。我也尝试了以下方法:

import subprocess
import time

binary_path = '/path/to/binary'
args = 'arguments' # arbitrary

call_str = '{} {}'.format(binary_path, args)

proc = subprocess.Popen(call_str)
time.sleep(5)
proc.terminate()
Run Code Online (Sandbox Code Playgroud)

此代码片段在终止程序时工作正常,因此问题不是发送到终止的实际信号。

如何更改代码以便子进程可以在 KeyboardInterrupt 上终止?

我正在运行 Python 2.7 和 Windows 7 64 位。提前致谢!

我尝试过的一些相关问题:

Python子进程Ctrl+C

在 KeyboardInterrupt 后杀死 subprocess.call

当python进程被杀死时杀死子进程?

lim*_*i44 5

我想出了一种方法来做到这一点,类似于让-弗朗索瓦对循环的回答,但没有多线程。关键是使用 Popen.poll() 来确定子进程是否已完成(如果仍在运行,将返回 None )。

import subprocess
import time

binary_path = '/path/to/binary'
args = 'arguments' # arbitrary

call_str = '{} {}'.format(binary_path, args)

proc = subprocess.Popen(call_str)

try:
    while proc.poll() is None:
        time.sleep(0.1)

except KeyboardInterrupt:
    proc.terminate()
    raise
Run Code Online (Sandbox Code Playgroud)

我在 KeyboardInterrupt 之后添加了一个额外的 raise,所以除了子进程之外,Python 程序也被中断了。

编辑:根据 eryksun 的评论将 pass 更改为 time.sleep(0.1) 以减少 CPU 消耗。

  • `time.sleep(.1)` 是可中断的。使用它代替“pass”应该可以减少 CPU 消耗。 (2认同)