1

I have a very long running process that I would like to call from a Python program. This process outputs a lot of information 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?

Jon
  • 3,985
  • 7
  • 48
  • 80
  • possible duplicate of [Non-blocking read on a subprocess.PIPE in python](http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python) – agf Sep 20 '11 at 01:13
  • 1
    This question is closely related. I think it solves your problem: http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python – Carl F. Sep 20 '11 at 00:42
  • So can I convert my own post to a comment or do I need some ridiculous number of points to do that? ;) – Carl F. Sep 20 '11 at 01:48
  • This is almost the same. The only difference is that the top answer in the thread you pointed me to will continue to spawn processes. I do want the process to block until it is finished, but I just want to see output along the way. – Jon Sep 20 '11 at 03:56

1 Answers1

1

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
Jon
  • 3,985
  • 7
  • 48
  • 80