Custom Formatting The Output Of subprocess.Popen

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thedsadude@gmail.com

    Custom Formatting The Output Of subprocess.Popen

    Hello,

    I'm launching a script as follows:
    <code>
    p = subprocess.Pope n(['./p.py', 'aa'])

    p.wait()
    </code>

    If p.py writes to sys.stdout, then it is shown on the console.
    Looking at the console, then, it is hard to distinguish the output of
    p.py from that of the script launching it. I'd like to do it so the
    output looks like this:
    <output>
    output of launcher
    p: output of launchee
    p: more output of launchee
    more output of launcher
    </output>
    i.e., that each output line of p.py will be formatted so that it is
    preceded by 'p:'.

    How should this be done, then? Should I write a file class, and pass
    on object like so:
    <code>
    custom_f = custom_file(sys .stdout, line_prefix = 'p')

    p = subprocess.Pope n(['./p.py', 'aa'], stdout = custom_f)

    p.wait()
    </code>
    or is a different option easier? Are there any links for writing
    custom file classes?

    Thanks & Bye,

    TD

  • Diez B. Roggisch

    #2
    Re: Custom Formatting The Output Of subprocess.Pope n

    thedsadude@gmai l.com wrote:
    Hello,
    >
    I'm launching a script as follows:
    <code>
    p = subprocess.Pope n(['./p.py', 'aa'])
    >
    p.wait()
    </code>
    >
    If p.py writes to sys.stdout, then it is shown on the console.
    Looking at the console, then, it is hard to distinguish the output of
    p.py from that of the script launching it. I'd like to do it so the
    output looks like this:
    <output>
    output of launcher
    p: output of launchee
    p: more output of launchee
    more output of launcher
    </output>
    i.e., that each output line of p.py will be formatted so that it is
    preceded by 'p:'.
    >
    How should this be done, then? Should I write a file class, and pass
    on object like so:
    <code>
    custom_f = custom_file(sys .stdout, line_prefix = 'p')
    >
    p = subprocess.Pope n(['./p.py', 'aa'], stdout = custom_f)
    >
    p.wait()
    </code>
    or is a different option easier? Are there any links for writing
    custom file classes?
    Why can't you just do?

    print "<output>"
    p = subprocess.Pope n(...)
    p.wait()
    print "</output>"

    ? If you wait for the subprocess to terminate anyway..

    Another way would be to use the p.communicate()-call to get the child's
    output, and print that to stdout yourself, pre/postfixing it as needed.

    Diez

    Comment

    • Scott David Daniels

      #3
      Re: Custom Formatting The Output Of subprocess.Pope n

      thedsadude@gmai l.com wrote:
      Hello,
      >
      I'm launching a script as follows:
      <code>
      p = subprocess.Pope n(['./p.py', 'aa'])
      >
      p.wait()
      </code>
      >
      If p.py writes to sys.stdout, then it is shown on the console....
      You seem to be missing the fact that ./py is run in a different process.
      The "sys.stdout " that p.py uses is different from that in the program
      calling Popen. In fact, it could be using a different Python. The
      situation is really similar to
      p = subprocess.Pope n([<basic program>, 'aa'])
      in that you have no way to "muck with the guts" of the subprocess, you
      can only post-process its output.

      --Scott David Daniels
      Scott.Daniels@A cm.Org

      Comment

      • thedsadude@gmail.com

        #4
        Re: Custom Formatting The Output Of subprocess.Pope n

        On Nov 21, 8:50 pm, Scott David Daniels <Scott.Dani...@ Acm.Orgwrote:
        thedsad...@gmai l.com wrote:
          Hello,
        >
          I'm launching a script as follows:
        <code>
        p = subprocess.Pope n(['./p.py', 'aa'])
        >
        p.wait()
        </code>
        >
          If p.py writes to sys.stdout, then it is shown on the console....
        >
        You seem to be missing the fact that ./py is run in a different process.
        The "sys.stdout " that p.py uses is different from that in the program
        calling Popen.  In fact, it could be using a different Python.  The
        situation is really similar to
             p = subprocess.Pope n([<basic program>, 'aa'])
        in that you have no way to "muck with the guts" of the subprocess, you
        can only post-process its output.
        >
        --Scott David Daniels
        Scott.Dani...@A cm.Org

        Hello,

        Thanks Diez & Scott for your replies.

        The subprocess.Pope n documentation states, concerning the stding,
        stdout, stderr, arguments, that:
        <quote>
        stdin, stdout and stderr specify the executed programs' standard
        input, standard output and standard error file handles, respectively.
        Valid values are PIPE, an existing file descriptor (a positive
        integer), an existing file object, and None. PIPE indicates that a new
        pipe to the child should be created. With None, no redirection will
        occur; the child's file handles will be inherited from the parent.
        Additionally, stderr can be STDOUT, which indicates that the stderr
        data from the applications should be captured into the same file
        handle as for stdout.
        </quote>
        so, it seems to me that if I would know how to write a file object,
        then I could write one that prefixes each line, and that would be
        fine, no? I don't see how this would necessitate waiting for p.py's
        termination, or matter that it is a different process. I just don't
        know how to create a file object.

        Thanks & Bye,

        TD

        Comment

        • Scott David Daniels

          #5
          Re: Custom Formatting The Output Of subprocess.Pope n

          thedsadude@gmai l.com wrote:
          ...
          so, it seems to me that if I would know how to write a file object,
          then I could write one that prefixes each line, and that would be
          fine, no? I don't see how this would necessitate waiting for p.py's
          termination, or matter that it is a different process. I just don't
          know how to create a file object.
          Duck typing to the rescue: Here's a simple file object for writing:

          class MyFunkyOutput(o bject):
          def write(self, sometext):
          print repr(sometext)
          def close(self):
          pass

          outputter = MyFunkyOutput()
          print >>outputter, 1,'a', 43

          Comment

          • Steve Holden

            #6
            Re: Custom Formatting The Output Of subprocess.Pope n

            thedsadude@gmai l.com wrote:
            On Nov 21, 8:50 pm, Scott David Daniels <Scott.Dani...@ Acm.Orgwrote:
            >thedsad...@gma il.com wrote:
            >> Hello,
            >> I'm launching a script as follows:
            >><code>
            >>p = subprocess.Pope n(['./p.py', 'aa'])
            >>p.wait()
            >></code>
            >> If p.py writes to sys.stdout, then it is shown on the console....
            >You seem to be missing the fact that ./py is run in a different process.
            >The "sys.stdout " that p.py uses is different from that in the program
            >calling Popen. In fact, it could be using a different Python. The
            >situation is really similar to
            > p = subprocess.Pope n([<basic program>, 'aa'])
            >in that you have no way to "muck with the guts" of the subprocess, you
            >can only post-process its output.
            >>
            >--Scott David Daniels
            >Scott.Dani...@ Acm.Org
            >
            >
            Hello,
            >
            Thanks Diez & Scott for your replies.
            >
            The subprocess.Pope n documentation states, concerning the stding,
            stdout, stderr, arguments, that:
            <quote>
            stdin, stdout and stderr specify the executed programs' standard
            input, standard output and standard error file handles, respectively.
            Valid values are PIPE, an existing file descriptor (a positive
            integer), an existing file object, and None. PIPE indicates that a new
            pipe to the child should be created. With None, no redirection will
            occur; the child's file handles will be inherited from the parent.
            Additionally, stderr can be STDOUT, which indicates that the stderr
            data from the applications should be captured into the same file
            handle as for stdout.
            </quote>
            so, it seems to me that if I would know how to write a file object,
            then I could write one that prefixes each line, and that would be
            fine, no? I don't see how this would necessitate waiting for p.py's
            termination, or matter that it is a different process. I just don't
            know how to create a file object.
            >
            It was an astute observation that you could provide your own "file-like
            object" as the output for the subprocess. However, you may find that due
            to buffering effects the subprocess's output doesn't get completely
            intermingled with the calling process's output.

            Although you can specify "unbuffered ", which is the default for
            subprocess.pope n, I don't believe this guarantees that the subprocess
            itself won't perform buffering. Others may know better.

            regards
            Steve
            --
            Steve Holden +1 571 484 6266 +1 800 494 3119
            Holden Web LLC http://www.holdenweb.com/

            Comment

            Working...