subprocess -popen - reading stdout from child - hangs

Karthik Gurusamy kar1107 at gmail.com
Sun Sep 23 02:58:02 EDT 2007


On Sep 22, 8:28 pm, "gregpin... at gmail.com" <gregpin... at gmail.com>
wrote:
> Let's say I have this Python file called loop.py:
>
> import sys
> print 'hi'
> sys.stdout.flush()

Add sys.stdout.close()

> 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()
>
> proc.communicate() also just hangs.  What am I doing wrong?  Please
> advise.

Since your loop.py is still alive and hasn't closed its stdout, the
caller continues to wait for EOF (it doesn't know if loop.py is done
generating all its output)

Karthik

>
> Thanks,
>
> Greg





More information about the Python-list mailing list