Interacting with Subprocesses

Akira Li 4kir4.1i at gmail.com
Wed May 4 17:04:02 EDT 2016


Dick Holmes <encore1 at cox.net> writes:

> I am attempting to write a Python program that will interact with
> a (non-Python) process. The programs will run under MinGW. The
> process can use stdin/stdout commands and responses and can work 
> with pipes. The problem I'm having is that I can't find any
> way in Python to have a continuing dialog with the process. I
> have tried Popen communicate, but that protocol seems to be
> limited to a single message/response pair, and the response
> is not returned to the message originator until the process
> terminates. Unfortunately I don't have access to the process'
> source code so I can't change the communication medium.
>
> Is there some feature that will allow me to initiate the process
> and execute multiple message/response pairs between the Python
> program and the process during a single execution of the process?
>

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

btw, If pexpect module works in MingGW environment (if pty is
available); you could try it to communicate with the process
interactively.

You might also find the list of Stackoverflow question related to the
subprocess module useful http://stackoverflow.com/tags/subprocess/info


Akira




More information about the Python-list mailing list