遵循如何终止使用 shell=True 启动的 python 子进程的建议
我有一个过程,我从以下开始
process = subprocess.Popen(my_cmd, shell=True, executable='/bin/bash', preexec_fn=os.setsid)
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)
杀人后,我收到以下警告
ResourceWarning: subprocess XXX is still running
为什么我会收到此警告?
你没有wait
这个过程结束。即使你杀死了这个过程,你仍然应该为它wait
,这样你就不会有一个僵尸进程在周围徘徊。Python 警告你,你没有wait
.
杀死它后添加一个process.wait()
调用。