Running an interactive subprocess with Popen

norseman norseman at hughes.net
Fri Apr 10 19:33:49 CEST 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



More information about the Python-list mailing list