小编ENZ*_*SIO的帖子

Ctrl C不会杀死Python中循环的子进程

是否有适当的方法来创建一个脚本,该脚本循环遍历文件夹中的文件并执行可以用Ctrl C在外部终止的子进程?我在管道中嵌入了类似以下内容的内容,当主进程被杀死时,无法从命令行Ctrl C进行操作。

示例脚本:

import subprocess
import os
import sys

input_directory = sys.argv[1]

for file in os.listdir(os.path.abspath(input_directory)):
    output = file + "_out.out"
    command = ['somescript.py', file, output]
    try:
        subprocess.check_call(command)
    except:
        print "Command Failed"
Run Code Online (Sandbox Code Playgroud)

然后我将执行程序:

Example_script.py /path/to/some/directory/containing/files/
Run Code Online (Sandbox Code Playgroud)

在循环时,如果我看到命令失败,我想使用CtrlC。但是,尽管主脚本已被Ctrl C销毁,但它仍然失败并继续运行其他子进程。是否有适当的方式编写这样的代码可以使用Ctrl C杀死孩子(其他子过程)?

任何帮助,或指出我的方向,不胜感激。我目前正在寻找一种好方法。

python subprocess kill system-calls

3
推荐指数
1
解决办法
113
查看次数

标签 统计

kill ×1

python ×1

subprocess ×1

system-calls ×1