我只是想了解在处理subprocess.Popen()结果和逐行读取时内存使用方面在“背景”中发生的情况 . 这是一个简单的例子 .
给定以下脚本 test.py
打印"Hello"然后等待10s并打印"world":
import sys
import time
print ("Hello")
sys.stdout.flush()
time.sleep(10)
print ("World")
然后,以下脚本 test_sub.py
将作为子进程'test.py'调用,将stdout重定向到管道,然后逐行读取:
import subprocess, time, os, sy
cmd = ["python3","test.py"]
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, universal_newlines = True)
for line in iter(p.stdout.readline, ''):
print("---" + line.rstrip())
在这种情况下,我的问题是,当我在执行子进程调用后运行 test_sub.py
时,它将打印"Hello"然后等待10秒直到"world"来然后打印它, what happens to "Hello" during those 10s of waiting? Does it get stored in memory until test_sub.py finishes, or does it get tossed away in the first iteration?
对于这个例子来说,这可能并不重要,但是在处理真正大的文件时它确实如此 .
1 回答
"Hello"
(在父级中)可以通过line
名称获得,直到.readline()
返回第二次,即"Hello"
至少存在,直到在父级中读取print("World")
的输出 .如果您的意思是在子进程中发生了什么,那么在
sys.stdout.flush()
之后"Hello"
对象没有理由继续生存,但它可能会例如,见Does Python intern strings?.readline()
第二次返回后,line
指的是"World"
. 之后"Hello"
会发生什么情况取决于特定Python实现中的垃圾收集,即使line
是"World"
;对象"Hello"
可能会继续存在一段时间 . Releasing memory in Python .您可以设置
PYTHONDUMPREFS=1
envvar并使用debugpython
build运行代码,以查看python
进程退出时处于活动状态的对象 . 例如,考虑以下代码:它表明
line
直到第二个yield
才会反弹 .PYTHONDUMPREFS=1 ./python . |& grep "'hello'"
的输出显示python
在python
退出时仍处于活动状态 .