subprocess -popen - reading stdout from child - hangs
Lawrence D'Oliveiro
ldo at geek-central.gen.new_zealand
Sun Sep 23 22:53:11 EDT 2007
In message <1190518129.356844.242710 at 19g2000hsx.googlegroups.com>,
gregpinero at gmail.com wrote:
> Let's say I have this Python file called loop.py:
>
> import sys
> print 'hi'
> sys.stdout.flush()
> while 1:
> pass
>
> And I want to call it from another Python process and read the value
> 'hi'. How would I do it?
>
> So far I have tried this:
>
>>>> proc = subprocess.Popen('python
>>>> /home/chiefinnovator/loop.py',shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE)
>>>> proc.stdout.read()
>
> But it just hangs at read()
That's because you didn't tell it how much to read, so by default it tries
to read until EOF <http://docs.python.org/lib/bltin-file-objects.html>. But
since the subprocess is still running and hasn't closed its stdout, the
pipe has not reached EOF.
More information about the Python-list
mailing list