[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
getting
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.
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.
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()
- Jeff Younker - jeff at drinktomi.com -
More information about the Tutor
mailing list