Subprocess not working with stdin/stdout set to PIPE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benny the Guard
    New Member
    • Jun 2007
    • 92

    Subprocess not working with stdin/stdout set to PIPE

    I am trying to use the subprocess module to run an interactive tool, issue some commands via stdin, then read the results from stdout and end.

    So I do the following:

    Code:
    output = Popen(["mycmd", "myarg"], stdin=PIPE,stdout=PIPE).communicate('prompt command')[0]
    mycmd should load, write output to stdout, then prompt for data. When data is given it prints some more. Well when I call it the prompt command never gets called and output contains only the init prints. If I set stdout to None, the prompt command runs and everything prints out as intended. In this case its printed to the screen, not to the output.

    So why does it break when I try to pipe the output? I am using Python 2.6.2.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You are missing a comma after stdout=PIPE for starters. Also, you may have to flush stdout. There aren't any write statements but I assume that is in some other part of your code. With just the one line there is very little to be done. First, see Doug Hellmann's examples here http://blog.doughellmann.com/2007/07...ubprocess.html. Reading and writing is about 1/3 of the way down the page. Second, if you want further help, come up with a simple program that reads and writes from a list (or that is supposed to read and write) and post it here so there is some specific code to help with.

    Comment

    • Benny the Guard
      New Member
      • Jun 2007
      • 92

      #3
      Not sure I see the need for a trailing comma, its the last param so no need for it. And the program does run, just not correctly.

      I tried to simplify my code and likely did too much. So here is my code.

      Code:
      proc = subprocess.Popen('myprogram',
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE
                             )
      (stdoutdata, stderrdata) = proc.communicate('print hello')
      print str(stdoutdata)
      When this runs the print does print out the programs startup messages (just some diag prints about loading config files, etc. which takes about 15-20secs) and that's all. The subprocess hould also be creating its own log files but nothing is in there, its as if it loads the program then exits without applying code to the stdif. If I do the following:

      Code:
      proc = subprocess.Popen('myprogram',
                             stdin=None,
                             stdout=subprocess.PIPE
                             )
      (stdoutdata, stderrdata) = proc.communicate('print hello')
      print str(stdoutdata)
      I see the startup messages, plus the output of running he 'print hello' input to the prompt and everything is as expected including the subprocess logs. Why does it not work when I redirect to a variable?

      Comment

      • Benny the Guard
        New Member
        • Jun 2007
        • 92

        #4
        Sorry copied the wrong test code for the second example, that works. The code that acts as it should is

        Code:
        proc = subprocess.Popen('myprogram',
                                stdin=subprocess.PIPE,
                                stdout=None
                                )
        (stdoutdata, stderrdata) = proc.communicate('print hello')
        print str(stdoutdata)
        That is stdout is None not stdin, which of course would not work since stdin would not do anything.

        Comment

        Working...