Help with pipes, buffering and pseudoterminals

Daniel Ellis ellisd23 at gmail.com
Sun Apr 5 21:20:48 CEST 2015


I have a small little tool I'd like to make.  It essentially takes piped input, modifies the text in some way, and immediately prints the output.  The problem I'm having is that any output I pipe to the program seems to be buffered, removing the desired effect.

>From what I understand, I need to somehow have the input be retrieved via a pseudoterminal.  The problem that I'm having is that most examples on the internet seem to assume I would like to launch a program in a forked pty process, which doesn't really fit my use case.

I've tried a number of things, but I seem to be unable to get even a basic understanding of how to use the pty module.  Here's a piece of code I whipped up just to try to get a feel for what is going on when I use pty.fork, but it doesn't seem to do what I think it should:

    import pty 
    import os
    import sys 

    pid, fd = pty.fork()

    print pid, fd
    sys.stdout.flush()

    os.read(fd, 1024)

This only seems to print from the parent process.  I read that I need to do the os.read call for the fork to happen.  I've also tried printing *after* the os.read call.  

I realize this does very little to solve my overall goal, but I figure understanding what is going on is probably a worthwhile first step.



More information about the Python-list mailing list