Python 3 通过发送 Ctrl C 停止子进程

Laz*_*ggy 13 python linux gpu python-3.x

我有一些 GPU 测试软件,我正在尝试使用 python3 自动化,测试通常会运行 3 分钟,然后由用户使用 ctrl+c 取消,生成以下输出

GPU测试输出

使用 ctrl+c 退出后,可以再次运行测试,没有问题

当尝试使用子进程 popen 并发送 SIGINT 或 SIGTERM 来自动执行此操作时,我得到的结果与使用键盘输入时的结果不同。脚本突然退出,并且在后续运行中找不到 GPU(假设它没有正确卸载驱动程序)

from subprocess import Popen, PIPE
from signal import SIGINT
from time import time


def check_subproc_alive(subproc):
    return subproc.poll() is None

def print_subproc(subproc, timer=True):
    start_time = time()
    while check_subproc_alive(subproc):
        line = subproc.stdout.readline().decode('utf-8')
        print(line, end="")
        if timer and (time() - start_time) > 10:
            break


subproc = Popen(['./gpu_test.sh', '-t', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)

print_subproc(subproc)

subproc.send_signal(SIGINT)

print_subproc(subproc, False)
Run Code Online (Sandbox Code Playgroud)

如何将 ctrl+c 发送到子进程,就像用户键入它一样?

**更新

import subprocess


def start(executable_file):
    return subprocess.Popen(
        executable_file,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )


def read(process):
    return process.stdout.readline().decode("utf-8").strip()


def write(process):
    process.stdin.write('\x03'.encode())
    process.stdin.flush()

def terminate(process):
    process.stdin.close()
    process.terminate()
    process.wait(timeout=0.2)


process = start("./test.sh")
write(process)
for x in range(100):
    print(read(process))
terminate(process)
Run Code Online (Sandbox Code Playgroud)

尝试了上面的代码,可以获取字符来注册虚拟 sh 脚本,但是发送 \x03 命令只是发送一个空字符并且不会结束脚本

Jon*_*n S 10

我想你可能可以使用这样的东西:

import signal
try:
    p=subprocess...
except KeyboardInterrupt:
    p.send_signal(signal.SIGINT)
Run Code Online (Sandbox Code Playgroud)


小智 3

以下解决方案是我能找到的唯一适用于Windows的解决方案,并且与发送Ctrl+C事件最相似。

import signal
os.kill(self.p.pid, signal.CTRL_C_EVENT)
Run Code Online (Sandbox Code Playgroud)