Python wrapper, problem with subprocess read/write

Discussion in 'Python' started by NeoGregorian, Sep 7, 2007.

  1. NeoGregorian

    NeoGregorian Guest

    Hello, I am writing a wrapper to a basic Input/Output programs (where
    you type a one line command at a time and then get 0 or more lines of
    output before you can input the next command).

    I'm sorry if this problem description is a bit long, but I wanted to
    make the problem clear.

    Example run of the original program:
    C:\home\>start
    Starting up program
    [Welcome to program v.X.Y.Z]
    blahblahblah
    and some more lines
    >input command

    program response...
    more program response...
    etc.
    >another command

    ....

    This is what the wrapper is expected to do...

    1: Start up the program.
    2: Forward the startup printouts by the program until the line where
    it first asks for a command.
    3: When waiting for input, input a command from a sequential list of
    strings (or other source of strings).
    4: Forward the programs printouts until all lines are read and the
    program prompts for new command.
    5: Repeat 3-4 until list is depleted or program is terminated and then
    close the program.


    Now, to the problem:
    In step 2/4, how to read all lines except the one which is unfinished
    (in the example, the lines beginning with >) and waiting for input?

    My attempts use something like this:

    proc = Popen(['programname'], stdout = PIPE, stdin = PIPE )
    for string_element in string_source :
    proc.stdin.write(string_element)
    lines = proc.stdout.readlines()
    method_that_processes_output(lines)

    The problem with this is that stdout.readlines() doesn't return since
    it reads until EOF...
    I tried instead to use:

    lines = []
    line = proc.stdout.readline()
    while line :
    lines.append(line)
    line = proc.stdout.readline()

    This prints out everything except the ">" line, which is good. But
    then freezes while waiting for input, which is bad.

    Any suggestions on how to solve this in a good way?
     
    NeoGregorian, Sep 7, 2007
    #1
    1. Advertising

  2. NeoGregorian

    A.T.Hofkamp Guest

    On 2007-09-07, NeoGregorian <> wrote:
    > I tried instead to use:
    >
    > lines = []
    > line = proc.stdout.readline()
    > while line :
    > lines.append(line)
    > line = proc.stdout.readline()
    >
    > This prints out everything except the ">" line, which is good. But
    > then freezes while waiting for input, which is bad.
    >
    > Any suggestions on how to solve this in a good way?


    'readline()' reads a line, that is, some text ending with a new-line. Since
    your last line, the ">" prompt has no ending new-line, the call blocks, waiting
    for the new-line character.

    So the simple anser is "don't use readline()".

    You have to fall back to reading characters, such as "read(1)" (which block
    until it receives a character).
    In addition, you will have to do analysis on whether the line you are currently
    reading is a prompt, and if so, stop reading to prevent blocking.
    (and instead, give the program a command by writing to proc.stdin).


    In case you don't know, pexpect (Python expect) does all (and more) that you
    are trying to do.


    Sincerely,
    Albert
     
    A.T.Hofkamp, Sep 10, 2007
    #2
    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. franco ziade

    how to write a wrapper function?

    franco ziade, Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    3,318
    Michael Mair
    Feb 18, 2005
  2. Chris Bordeman
    Replies:
    8
    Views:
    498
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  3. hiral
    Replies:
    2
    Views:
    605
    Jean-Michel Pichavant
    May 5, 2010
  4. Vinay Sajip
    Replies:
    0
    Views:
    110
    Vinay Sajip
    Jun 4, 2013
  5. Vinay Sajip
    Replies:
    0
    Views:
    99
    Vinay Sajip
    Dec 17, 2013
Loading...

Share This Page