subprocess.Popen objects: how to ensure that stdin is closed

Harishankar v.harishankar at gmail.com
Fri Apr 2 06:26:05 EDT 2010


On Fri, 02 Apr 2010 10:17:55 +0000, Harishankar wrote:

> I am writing a small app which requires input using stdin to the
> subprocess.
> 
> I use the following technique:
> 
> proc = subprocess.Popen (cmdargs, stdin=subprocess.PIPE)
> 
> proc.stdin.write ("Something")
> proc.stdin.flush ()
> ...
> proc.stdin.write ("something else")
> proc.stdin.flush ()
> ...
> 
> and so on. I cannot use communicate() because it waits till program
> termination and so obviously can be used once only.
> 
> The problem is that I want to close the process and it's not responding
> either to proc.stdin.close() or even proc.terminate() which is in Python
> 2.6 (not in 2.5.x)
> 
> So I am left with a mangled terminal.
> 
> Is subprocess behaving funny or am I doing something wrong? I am not
> even sure if the proc.stdin.close () is respected because even without
> it, I am getting the same mangled state. I just want to control the
> commands using stdin.write and then close the process when done.

Hmm...

just two minutes after I posted this. I just added this

proc.wait ()

after closing stdin and it works fine now. Still not sure whether I need 
the proc.stdin.close () though.





-- 
Harishankar (http://harishankar.org http://literaryforums.org)



More information about the Python-list mailing list