检查子进程是否被用户终止

Checking if subprocess was terminated by user

提问人:Mohamed Darwesh 提问时间:3/9/2023 最后编辑:Mohamed Darwesh 更新时间:3/9/2023 访问量:32

问:

# Create a new terminal window and run the ffmpeg command in it
cmd = ' '.join(ffmpeg.compile(output_stream, overwrite_output=True))
compressing = subprocess.Popen('start cmd /k {}'.format(cmd), shell=True)

while compressing.poll() is None: #check if the process is still running
    print('Processing video...')
    break
else:
    if compressing.returncode == 0: #check if the process was canceled by the user
       print('Video processing canceled by user!')
    else: #if the process was completed successfully
        print('Video processing complete!')

我想检查进程(终端)是否被用户关闭,它应该打印该句子,但是即使用户杀死了子进程终端,它也总是卡住了"Processing Video"

python shell 子进程 subshell

评论

1赞 chepner 3/9/2023
你的循环永远不会使用那里的无条件语句多次迭代。你为什么有它?break
0赞 Mohamed Darwesh 3/9/2023
@chepner 这是一个更大代码的简化版本,但是我希望文本只出现一次以节省资源,并且由于某种原因,如果没有 break 语句,程序将直接转到 if 语句(该进程仍在运行)
0赞 chepner 3/9/2023
我认为这是因为您没有监控在后台运行的 ,而是(或执行的 shell),它在启动后立即终止。cmdstartstartcmd
0赞 chepner 3/9/2023
为什么不跳过 shell 并直接用作运行命令呢?list(ffmpeg.compile(...))
0赞 Mohamed Darwesh 3/9/2023
@chepner 可能是我正在监视外壳,我确实必须检查一下。但是,我使用的是 shell,因为这是 GUI 程序的一部分,因此一旦用户按下按钮,它应该打开一个新终端并向他显示进度。这就是为什么

答: 暂无答案