Running an interactive subprocess with Popen

Discussion in 'Python' started by David Liang, Apr 10, 2009.

  1. David Liang

    David Liang Guest

    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 Liang, Apr 10, 2009
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Apr 10, 2009
    #2
    1. Advertising

  3. David Liang

    edexter Guest

    On Apr 10, 2:56 am, "Diez B. Roggisch" <> 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
    edexter, Apr 10, 2009
    #3
  4. David Liang

    norseman Guest

    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
    norseman, Apr 10, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. WJ
    Replies:
    2
    Views:
    484
  2. Michele Simionato

    subprocess.Popen

    Michele Simionato, Dec 12, 2004, in forum: Python
    Replies:
    5
    Views:
    17,651
  3. Ivo Woltring
    Replies:
    4
    Views:
    3,552
    Jeff Shannon
    Jan 27, 2005
  4. Roman Medina-Heigl Hernandez

    Subprocess module: running an interactive shell

    Roman Medina-Heigl Hernandez, Mar 14, 2009, in forum: Python
    Replies:
    3
    Views:
    1,548
    Karthik Gurusamy
    Mar 15, 2009
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    229
    Robert Klemme
    May 20, 2006
Loading...

Share This Page