Running an interactive subprocess with Popen

D

David Liang

Hello,
Sorry for the newbie question. How do I run a program that could
block, waiting for user input, using subprocess.Popen? For example,

from subprocess import *

def foo():
a = Popen(['python'] ...)

I want to be able to get input from the user and send input to the
subprocess, printing stdout and stderr as data becomes available, then
return once the subprocess exits. Is it possible to send to the
subprocess keyboard interrupts, EOF, and such?

I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using
communicate(), but could only call it once; subsequent calls raised
"ValueError: I/O operation on closed file."
And both a.stdout.read() and a.stderr.read() blocked the program. Any
help would be much appreciated.

-David
 
D

Diez B. Roggisch

David said:
Hello,
Sorry for the newbie question. How do I run a program that could
block, waiting for user input, using subprocess.Popen? For example,

from subprocess import *

def foo():
a = Popen(['python'] ...)

I want to be able to get input from the user and send input to the
subprocess, printing stdout and stderr as data becomes available, then
return once the subprocess exits. Is it possible to send to the
subprocess keyboard interrupts, EOF, and such?

I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using
communicate(), but could only call it once; subsequent calls raised
"ValueError: I/O operation on closed file."
And both a.stdout.read() and a.stderr.read() blocked the program. Any
help would be much appreciated.


Use pexpect, which emulates a pseudo-terminal speaking with the subprocess.

Diez
 
E

edexter

David Liang schrieb:




Hello,
Sorry for the newbie question. How do I run a program that could
block, waiting for user input, using subprocess.Popen? For example,
from subprocess import *
def foo():
    a = Popen(['python'] ...)
I want to be able to get input from the user and send input to the
subprocess, printing stdout and stderr as data becomes available, then
return once the subprocess exits. Is it possible to send to the
subprocess keyboard interrupts, EOF, and such?
I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using
communicate(), but could only call it once; subsequent calls raised
"ValueError: I/O operation on closed file."
And both a.stdout.read() and a.stderr.read() blocked the program. Any
help would be much appreciated.

Use pexpect, which emulates a pseudo-terminal speaking with the subprocess.

Diez- Hide quoted text -

- Show quoted text -

I am not sure about that particular issue put I did manage to get it
working with windows in the dex tracker project.. you would have to
search the files or something to figure out where.. when you are
looking at examples it does matter wich operating system you are using
at least for python 2.5
 
N

norseman

David said:
Hello,
Sorry for the newbie question. How do I run a program that could
block, waiting for user input, using subprocess.Popen? For example,

from subprocess import *

def foo():
a = Popen(['python'] ...)

I want to be able to get input from the user and send input to the
subprocess, printing stdout and stderr as data becomes available, then
return once the subprocess exits. Is it possible to send to the
subprocess keyboard interrupts, EOF, and such?

I tried doing stdout=PIPE, stderr=PIPE, stdin=PIPE. I tried using
communicate(), but could only call it once; subsequent calls raised
"ValueError: I/O operation on closed file."
And both a.stdout.read() and a.stderr.read() blocked the program. Any
help would be much appreciated.

-David
============================================
David;
Take a look at Section 14.1.2 of the Python Library pdf reference. The
popen3 should interest you. It allows 2 way communication with a
'child' process.
I was going to include some samples, but it seems I took them off when
that project terminated. (So they are buried somewhere in storage - sorry.)

I'm running Ver. 2.5.2 for mass compatibility.

Note:
I never tried redirection of the mouse. Just the keyboard, which
worked very well for me. In my case "Parent" controlled conversation.
In essence, treating 'child' as a subroutine. 'Child' included Unix
scripts, Unix programs, and some third party programs. Try to have
your "parent" program NOT talk to the screen while in this mode. :)


Steve
 

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

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top