Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Running an interactive subprocess with Popen

Reply
Thread Tools

Running an interactive subprocess with Popen

 
 
David Liang
Guest
Posts: n/a
 
      04-10-2009
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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      04-10-2009
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
 
Reply With Quote
 
 
 
 
edexter
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 2:56*am, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
norseman
Guest
Posts: n/a
 
      04-10-2009
David Liang wrote:
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>

============================================
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subprocess module: running an interactive shell Roman Medina-Heigl Hernandez Python 3 03-15-2009 12:10 AM
File.popen/IO.popen hariwise@gmail.com Ruby 1 05-20-2006 08:20 AM
Interactive/Non-interactive ASPX ? WJ ASP .Net 2 02-26-2005 02:54 AM
non Interactive and Interactive AAH Computer Support 0 01-09-2005 04:09 PM
Interactive and non interactive AAH Computer Support 1 01-09-2005 04:01 PM



Advertisments