subprocess.Popen() redirecting to TKinter or WXPython textwidget???

Jeff Shannon jeff at ccvcorp.com
Thu Jan 27 14:15:09 EST 2005


Ivo Woltring wrote:

> The output of mencoder is not readable with readlines (i tried it) because
> after the initial informational lines You don't get lines anymore (you get a
> linefeed but no newline)
> The prints are all on the same line (like a status line)
> something like
> Pos:   3,1s     96f ( 0%)  42fps Trem:   0min   0mb  A-V:0,038 [171:63]

Hm, I'm inferring that what you mean is that you get a carriage return 
(ASCII 0x0C) but no linefeed (ASCII 0x0A) -- CR returns you to the 
beginning of the current line, and LF advances you to the next line.

Rather than using readlines(), you could simply read() a few 
characters (or a single character) at a time, buffering it yourself 
and passing it on when you see the CR.

You're likely to run into I/O blockage issues no matter how you do 
this, though -- even if you're reading a single character at a time, 
read(1) won't return until you've read that character, and if the 
program on the other end of the pipe isn't writing anything, then your 
app is stuck.  The "simple" way to do this is by using an i/o thread, 
which does something like this:

     buffer = []
     while 1:
         char = outpipe.read(1)
         if char == '\0x0A':
             notify_gui_thread( ''.join(buffer) )
             buffer = []
         else:
             buffer.append(char)
         if StopEvent.IsSet():
             raise CustomStopException

Note that you don't want to try to update your GUI widgets directly 
from the worker (i/o) thread -- very few GUI toolkits are threadsafe, 
so you need to make all GUI calls from a single thread.

Jeff Shannon
Technician/Programmer
Credit International







More information about the Python-list mailing list