Note that there are some explanatory texts on larger screens.

plurals
  1. POcommunicate() does not catch suprocess stdout
    text
    copied!<pre><code>import subprocess def ExecuteAndGetValue(cmd, v): PrintCmd(cmd, v) sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE) result = sub.communicate() sub.wait() return result import subprocess def ExecuteAndGetValue2(cmd, v): PrintCmd(cmd, v) sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE) result = sub.communicate()[0] return result [aa, err] = Util.ExecuteAndGetValue(cmd, args.v) # run half an hour, the output size is about 15MB [bb, err] = Util.ExecuteAndGetValue(cmd, args.v) # run half an hour, the output size is about 15MB print aa // aa is empty print bb // bb is empty </code></pre> <p><code>cmd</code> prints/outputs: stderr using <code>print &gt;&gt; sys.stderr</code> and stdout using <code>print</code> </p> <pre><code>result = [] for i in range(n): print &gt;&gt; sys.stderr, "T_index: " + str(T_index) bb = func(i) result.append(bb) print '\n'.join(result) </code></pre> <p>cmd has a loop in which print <code>stderr</code> info. At the end, it prints all output to <code>result</code></p> <p>(1) if <code>cmd</code> print smaller size string, it works fine. <code>aa</code> and <code>bb</code> shows correct results.</p> <p>(2) if <code>cmd</code> print a large size such as 10 Mb, <code>aa</code> and <code>bb</code> are empty.</p> <p>(3) based on the above tests, it looks to me that sub.communicate()[0] read stdout from Popen in real time? if it is a large stdout, it cannot read it? Could someone explain it a little more ?</p> <p>In addition, I would like to see stderr or error message in screen in real time. If I use the following, it does not show stderr in real time:</p> <pre><code>import subprocess def ExecuteAndGetValue(cmd, v): PrintCmd(cmd, v) sub = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = sub.communicate() print result[1] return result </code></pre> <p>How can I fix this problem? it might be related to buffer size? Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload