I just want to understand what happens in the "background" in terms of memory usage when dealing with a subprocess.Popen() result and reading line by line. Here's a simple example.
Given the following script test.py
that prints "Hello" then waits 10s and prints "world":
import sys
import time
print ("Hello")
sys.stdout.flush()
time.sleep(10)
print ("World")
Then the following script test_sub.py
will call as a subprocess 'test.py', redirect the stdout to a pipe and then read it line by line:
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())
In this case my question would be, when I run test_sub.py
after it does the subprocess call, it will print "Hello" then wait 10s until "world" comes and then print it, 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?
This may not matter to much for this example, but when dealing with really big files it does.