twisted reactor.spawnProcess get stdout w/o bufffering on windows
Question: I'm running an external process and I need to get the stdout immediately so I
can push it to a textview, on GNU/Linux I can use "usePTY=True" to get the
stdout by line, unfortunately usePTY is not available on windows.

I'm fairly new to twisted, is there a way to achieve the same result on
Windows with some twisted (or python maybe) magic stuff?


Answer: > on GNU/Linux I can use "usePTY=True" to get the stdout by line

Sort of! What `usePTY=True` actually does is create a PTY (a "pseudo-terminal" - the thing you always get when you log in to a shell on GNU/Linux unless you have a _real_ terminal which no one does anymore :) instead of a boring old pipe. A PTY is a lot like a pipe but it has some extra features - but more importantly for you, a PTY is strongly associated with interactive sessions (ie, a _user_) whereas a pipe is pretty strongly associated with programmatic uses (think `foo | bar` \- no user ever sees the output of `foo`).

This means that people tend to use existence of a PTY as stdout as a signal
that they should produce output in a timely manner - because a human is
waiting to see it. On the flip side, the existence of a regular old pipe as
stdout is taken as a signal that another program is consuming the output and
they should instead produce output in the most _efficient_ way possible.

What this **tends** to mean in practice is that if a program has a PTY then
_it_ will line buffer its output and if it has a pipe then _it_ will "block"
buffer its output (usually gather up about 4kB of data before writing any of
it) - because line buffering is less efficient.

The thing to note here is that it is the _program you are running_ that does
this buffering. Whether you pass `usePTY=True` or `usePTY=False` makes no
direct difference to that buffering: it is just a hint to the program you are
running what kind of output buffering it should do.

This means that you might run programs that block buffer even if you pass
`usePTY=True` and vice versa.

However... Windows doesn't have PTYs. So programs on Windows can't consider
PTYs as a hint for how to buffer their output.

I don't actually know if there is another hint that it is conventional for
programs to respect on Windows. I've never come across one, at least.

If you're lucky, then the program you're running will have _some_ way for you
to request line-buffered output. If you're running Python, then it does - the
`PYTHONUNBUFFERED` environment variable controls this, as does the `-u`
command line option (and I think they both work on Windows).

Incidentally, if you plan to pass binary data between the two processes, then
you probably also want to put stdio into binary mode in the child process as
well:

    
    
        import os, sys, mscvrt                                                                                                                  
        msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)                                                                                         
        msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)                                                                                        
        msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)           
    

