杀死 shell=True 进程导致 ResourceWarning: subprocess is still running

Ruf*_*fus 4 python subprocess

遵循如何终止使用 shell=True 启动的 python 子进程的建议

我有一个过程,我从以下开始

process = subprocess.Popen(my_cmd, shell=True, executable='/bin/bash', preexec_fn=os.setsid)
Run Code Online (Sandbox Code Playgroud)

my_cmd 的形式为 some_cmd_that_periodically_outputs_to_stdout | grep "some_fancy_regex" > some_output_file.txt

我用以下命令终止进程

os.killpg(os.getpgid(process.pid), signal.SIGKILL)
Run Code Online (Sandbox Code Playgroud)

杀死后,我收到以下警告

ResourceWarning: subprocess XXX is still running
Run Code Online (Sandbox Code Playgroud)

为什么我会收到此警告?

use*_*ica 6

你没有wait让这个过程结束。即使您终止了该进程,您仍然应该为此终止该进程,wait因此您不会有一个僵尸进程闲逛。Python 警告你,你没有wait

process.wait()杀死它后添加一个呼叫。