View output from long running external process called from Python
I have a very long running process that I would like to call from a Python program. This process outputs a lot of i开发者_JAVA百科nformation to stdout. I would like to see the output from my called program on the command line as it is running. I have read about Popen, and tried
p = Popen(cmd, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
and variants of this, but the output from cmd doesn't get displayed until cmd is finished running.
How do I view the output of cmd while cmd is running?
I have figured this out by using part of what was mentioned in the comments, and combined that with the following
# read line without blocking
while not p.poll():
try:
line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
pass # Do nothing
else: # got line
print line
精彩评论