Monitoring stdout in (more or less) real time

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Aug 29 03:21:20 EDT 2009


En Sat, 29 Aug 2009 03:28:26 -0300, <ivanko.rus at gmail.com> escribió:

> Hello to everyone! I am making a program that will be a GTK+ frontend to
> ffmpeg. Naturally, one of the main functions is parsing ffmpeg's output.
> It's pretty simple when I, for example, retrieve information about a file
> (the program finishes and I read the output). But it also needs to parse
> working ffmpeg's output (in order to retrieve the percentage, remaining
> time, etc.). So, actually what I do is Popen ffmpeg, and connect to its
> stdout. And as stdout is represented by a file object, it needs to be
> read(). The problem is that read() reads until EOF is reached, which
> doesn't exist while the program is running (the same goes with
> communicate()).
> So my question is: is there a way to retrieve the stdout without waiting
> the program to finish?

You don't have to read the complete output at once - you may process it  
line by line, I presume. I'd use a second thread to read the pipe and put  
the lines onto a Queue object; the main thread gets lines from the Queue  
when available.

-- 
Gabriel Genellina




More information about the Python-list mailing list