python - Killing shell=True 进程导致 ResourceWarning: subprocess is still running
问题描述
遵循如何终止使用 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()
杀死它后添加一个呼叫。
推荐阅读
- android - 用户关闭应用程序时如何重新启动应用程序?
- python - Python中的傅里叶变换集成
- python - 运行python脚本后台时,作业总是停止
- javascript - 我们如何在 Vue JS 中使用“name”或“id”属性值作为 data 属性?
- sql - SQL如何获得运行总和+当前值?
- python-3.x - 如何将 Databricks 中的多处理池与 pandas 一起使用
- f# - 警告 FS1178“不支持结构平等”
- firebase - 如何通过 uesr id 访问实时数据库
- r - 如何从多个 PDF 文件中提取以预定义字母开头的特定行
- javascript - 如何启用在 Electron 和 Svelte 中移动无框窗口的功能?