how to poll and exit a subprocess
<p>I'd like to start a process, wait 2 seconds and print out whatever is in the stderr and stdout pipes so far and then exit. Here is the code I have so far and it doesn't seem to work as hoped. What am I doing wrong?</p>

<p>There are 3 issues:</p>

<ul>
<li>The program as it stands prints out "done" then waits for the
suprocess to complete before printing out the first line. </li>
<li>As it stands, the script reads one line. How to read to the end of the current buffer?</li>
<li><p>will the subprocess exit if the calling script exits? If so, how should I modify the
function call so that the subprocess runs to completion even if the
calling script exits?</p>

<pre><code>cmdStr = "./stepper.py"
proc = subprocess.Popen(cmdStr, shell=True, bufsize=-1, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
print "polling"
time.sleep(2)
print "done"
print proc.stdout.readline()
</code></pre></li>
</ul>

<p>Here is what the stepper.py looks like:</p>

<pre><code>out = open("stepper.log", 'w')
for idx in range(3):
    time.sleep(2)
    print "Idx",idx
    sys.stdout.flush()
    out.write("%d\n"%(idx))
print "fnished"
out.write("cloing\n")
out.close()
</code></pre>
