Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108171
| From | eryk sun <eryksun@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Interacting with Subprocesses |
| Date | 2016-05-05 04:38 -0500 |
| Message-ID | <mailman.403.1462441170.32212.python-list@python.org> (permalink) |
| References | <MPG.3193e21979405e40989683@news.supernews.com> <871t5h4jst.fsf@gmail.com> <CACL+1at_6dYoo=_A87F2tJROkz3z3Q1-4ZOaC=u7WmcYJYTL6Q@mail.gmail.com> |
On Wed, May 4, 2016 at 4:04 PM, Akira Li <4kir4.1i@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.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Interacting with Subprocesses Dick Holmes <encore1@cox.net> - 2016-05-04 11:41 -0700 Re: Interacting with Subprocesses Akira Li <4kir4.1i@gmail.com> - 2016-05-05 00:04 +0300 Re: Interacting with Subprocesses Terry Reedy <tjreedy@udel.edu> - 2016-05-04 20:33 -0400 Re: Interacting with Subprocesses Akira Li <4kir4.1i@gmail.com> - 2016-05-05 04:05 +0300 Re: Interacting with Subprocesses eryk sun <eryksun@gmail.com> - 2016-05-05 04:38 -0500
csiph-web