2016-05-19 41 views
0

我只是想了解在處理subprocess.Popen()結果和逐行讀取時,在內存使用情況方面在「背景」中發生了什麼。這是一個簡單的例子。當從管道子進程中讀取行時的內存使用情況python

考慮下面的腳本test.py上打印「你好」,然後等待10秒,並打印「世界」:

import sys 
import time 
print ("Hello") 
sys.stdout.flush() 
time.sleep(10) 
print ("World") 

然後將下面的腳本test_sub.py將調用作爲子「test.py」,重定向到標準輸出管道,然後通過在線閱讀行:

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它的子進程調用之後,它會打印「你好」,然後等待10秒,直到「世界」的由來然後打印我t,在等待的10年中,「Hello」會發生什麼?它是否存儲在內存中,直到test_sub.py完成,還是在第一次迭代中被拋棄?

這對這個例子來說可能沒有多大關係,但是當處理真正的大文件時,

回答

1

在等待的那10秒內發生了什麼?

"Hello"(在父)可經由line名直到.readline()返回第二時間即直到print("World")輸出在父讀取至少是,"Hello"

如果你的意思是在子進程中會發生什麼sys.stdout.flush()之後再沒有任何理由爲"Hello"對象繼續生活,但它可能例如,見Does Python intern strings?

是否得到存儲在內存中,直到test_sub.py完成,還是它在第一次迭代中被拋棄?

.readline()第二次返回後,line指的是"World"。之後"Hello"會發生什麼取決於具體Python實現中的垃圾收集,即,即使line"World";對象"Hello"可能會繼續存在一段時間。 Releasing memory in Python

您可以設置PYTHONDUMPREFS=1 ENVVAR和使用調試python身材,看對象是活的python進程退出時運行代碼。例如,考慮下面的代碼:

#!/usr/bin/env python3 
import threading 
import time 
import sys 

def strings(): 
    yield "hello" 
    time.sleep(.5) 
    yield "world" 
    time.sleep(.5) 

def print_line(): 
    while True: 
     time.sleep(.1) 
     print('+++', line, file=sys.stderr) 

threading.Thread(target=print_line, daemon=True).start() 
for line in strings(): 
    print('---', line) 
time.sleep(1) 

它表明line是不會反彈,直到第二yieldPYTHONDUMPREFS=1 ./python . |& grep "'hello'" 的輸出顯示當退出python'hello'仍然存在。

相關問題