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

H

Harishankar

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.
 
H

Harishankar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top