Dialog with a process via subprocess.Popen blocks forever
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Thu Mar 1 07:17:25 EST 2007
En Wed, 28 Feb 2007 18:27:43 -0300, <bayer.justin at googlemail.com> escribió:
> Hi,
>
> I am trying to communicate with a subprocess via the subprocess
> module. Consider the following example:
>
>>>> from subprocess import Popen, PIPE
>>>> Popen("""python -c 'input("hey")'""", shell=True)
> <subprocess.Popen object at 0x729f0>
>>>> hey
>
> Here hey is immediately print to stdout of my interpreter, I did not
> type in the "hey". But I want to read from the output into a string,
> so I do
>
>>>> x = Popen("""python -c 'input("hey\n")'""", shell=True, stdout=PIPE,
>>>> bufsize=2**10)
>>>> x.stdout.read(1)
> # blocks forever
Blocks, or is the child process waiting for you to input something in
response?
> Is it possible to read to and write to the std streams of a
> subprocess? What am I doing wrong?
This works for me on Windows XP. Note that I'm using a tuple with
arguments, and raw_input instead of input (just to avoid a traceback on
stderr)
py> x=Popen(("python", "-c", "raw_input('hey')"), shell=True, stdout=PIPE)
py> x.stdout.read(1)
1234
'h'
py> x.stdout.read()
'ey'
I typed that 1234 (response to raw_input).
You may need to use python -u, or redirect stderr too, but what your real
problem is?
--
Gabriel Genellina
More information about the Python-list
mailing list