[pygtk] Updating a TextBuffer line by line
John Finlay
finlay at moeraki.com
Fri Jan 23 05:45:24 WST 2009
Daniel Roesler wrote:
> Howdy again,
>
> I am running into a problem displaying stdout from a subprocess
> command. I have a loop that checks to see if the subprocess is still
> active, then reads a line in from stdout and sends it to the text
> buffer. However, I can't seem to get my text buffer to print except
> when the command ends. Obviously, I'm missing some logic behind how
> loops work with pygtk.
>
> Here's my code:
> ---------------------------
> command = subprocess.Popen(cmd, stdout=subprocess.PIPE)
>
> while command.poll() is None:
> line = command.stdout.readline()
> self.txtbuffer.insert_at_cursor(line)
> ---------------------------
>
> Any ideas on how to read stdout line by line with subprocesses?
>
>
Don't use command.poll() rather use gobject.io_add_watch to register a
callback when data is available on the stdout pipe or the pipe is
closed. Something like:
gobject.io_add_watch(command.stdout, gobject.IO-IN | gobject.IO_HUP,
read_output)
def read_output(source, condition):
if condition == gobject.IO_IN:
line = source.readline()
....
if condition == gobject.IO_HUP:
....
return False
return True
John
More information about the pygtk
mailing list