Interacting with Subprocesses

eryk sun eryksun at gmail.com
Thu May 5 05:38:47 EDT 2016


On Wed, May 4, 2016 at 4:04 PM, Akira Li <4kir4.1i at gmail.com> wrote:
>
> Pass stdin=PIPE, stdout=PIPE and use p.stdin, p.stdout file objects to
> write input, read output from the child process.
>
> Beware, there could be buffering issues or the child process may change
> its behavior some other way when the standard input/output streams are
> redirected. See
> http://pexpect.readthedocs.io/en/stable/FAQ.html#whynotpipe

On Linux, you may be able to use stdbuf [1] to modify standard I/O
buffering. stdbuf sets the LD_PRELOAD [2] environment variable to load
libstdbuf.so [3]. For example, the following shows the environment
variables created by "stdbuf -oL":

    $ stdbuf -oL python -c 'import os;print os.environ["LD_PRELOAD"]'
    /usr/lib/coreutils/libstdbuf.so
    $ stdbuf -oL python -c 'import os;print os.environ["_STDBUF_O"]'
    L

[1]: http://www.gnu.org/software/coreutils/manual/html_node/stdbuf-invocation.html
[2]: http://www.linuxjournal.com/article/7795
[3]: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/libstdbuf.c?id=v8.21

On Windows, if you can modify the program, then you can check for a
command-line option or an environment variable, like Python's -u and
PYTHONUNBUFFERED.

If you can't modify the source, I think you might be able to hack
something similar to the Linux LD_PRELOAD environment variable by
creating a stdbuf.exe launcher that debugs the process and injects a
DLL after the loader's first-chance breakpoint. The injected
stdbuff.dll would need to be able to get the standard streams for
common CRTs, such as by calling __acrt_iob_func for ucrtbase.dll.
Also, unlike the Linux command, stdbuf.exe would have to wait on the
child, since the Windows API doesn't have fork/exec.



More information about the Python-list mailing list