[Tutor] logic for a tree like structure

Tiago Saboga tiagosaboga at terra.com.br
Sun Dec 9 14:22:12 CET 2007

On Sat, Dec 08, 2007 at 04:13:19PM -0800, Jeff Younker wrote:
> Pipes and IO channels are buffered.   The buffers are much larger than
> the amount of information you are writing to them, so they're never getting
> flushed while the program is running.   The child program completes, the
> IO channel closes, and it flushes out the output.

Yes! I've just noticed that if I replaced the python writer.py with an
equivalent bash script it worked as expected (my original example).
And then, I could make it work with writer.py if I flush sys.stdout
after each print statement. It is a little frustrating to see
that I was not able to see something that already bit me other times,
but... c'est la vie...

> My advice is to forget about all the file descriptor and pipes stuff.  
> Getting
> incremental IO from them via the subprocess module (or any standard IO
> module) is painful.   You're probably better off getting the nonstandard
> pexpect module and using that instead.

Thanks, it sounds like a useful module (and I don't mind it being
nonstandard, as long as it is in debian - and it is ;) ) but I still
can't see how to do something like that in my case. As I mentioned in
another mail, I have two independent classes, one for the gui and the
other for the converter. The converter has to spawn several processes
depending on the return code of each of them and then do some clean
up. In this mean time (it can take hours), the gui must be alive and
updated with the stdout of the processes.


> Here's your program using pexpect.
> #/usr/bin/python2.5
> import pexpect
> def main():
>    cmd = pexpect.spawn('./writer.py')
>    try:
>        while True:
>            # wait for the end of the line or ten seconds
>            cmd.expect('\r\n', timeout=10)
>            # get output preceding the EOF matched above
>            line = cmd.before
>            print '[main]', line
>    except pexpect.TIMEOUT:
>          print "No output received for ten seconds"
>    except pexpect.EOF:
>          pass
>    finally:
>         cmd.close()
> if __name__ == '__main__':
>   main()

More information about the Tutor mailing list