[Tutor] logic for a tree like structure

Jeff Younker jeff at drinktomi.com
Sun Dec 9 01:13:19 CET 2007

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  
flushed while the program is running.   The child program completes, the
IO channel closes, and it flushes out the output.

My advice is to forget about all the file descriptor and pipes stuff.   
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.

Here's your program using pexpect.


import pexpect

def main():
    cmd = pexpect.spawn('./writer.py')
        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:

if __name__ == '__main__':

- Jeff Younker - jeff at drinktomi.com -

More information about the Tutor mailing list