Simultaneous output parsing of child process?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fraser Gordon

    #1

    Simultaneous output parsing of child process?

    Hello,

    Hopefully someone can help me out with this issue...

    I have a python script that needs to run a shell command and be able
    to react to output from that command as it occurs (as opposed to
    waiting for the whole command to complete and then parsing the
    output). Is this possible with Python?

    I created a test script that prints a line of output to stdout every 4
    seconds or so. I want to create a "parent" script that kicks off the
    test script, and prints data as soon as the test script sends anything
    to the standard output (ie. every 4 seconds).

    Furthermore, the solution needs to support Windows, several flavors of
    UNIX and potentially LINUX.

    I would ideally like to have the following type of behaviour:

    childPipe = kickOffChild('t est.py')
    while <child process still active>:
    line = childPipe.check ForStdOutput()
    if line: # This should trigger once every 4 seconds in the
    example above
    print "OUTPUT:", line
    print "Process completed."


    Any help would be greatly appreciated.

    Fraser
  • Emile van Sebille

    #2
    Re: Simultaneous output parsing of child process?


    "Fraser Gordon" <fraser.gordon@ cognos.com> wrote in message
    news:deb65cae.0 310020645.3af9c f62@posting.goo gle.com...[color=blue]
    > Hello,
    >
    > Hopefully someone can help me out with this issue...
    >
    > I have a python script that needs to run a shell command and be able
    > to react to output from that command as it occurs (as opposed to
    > waiting for the whole command to complete and then parsing the
    > output). Is this possible with Python?
    >
    > I created a test script that prints a line of output to stdout every 4
    > seconds or so. I want to create a "parent" script that kicks off the
    > test script, and prints data as soon as the test script sends anything
    > to the standard output (ie. every 4 seconds).
    >
    > Furthermore, the solution needs to support Windows, several flavors of
    > UNIX and potentially LINUX.
    >[/color]


    Perhaps something that Donn Cave posted a few years ago helps, particularly
    the bonus version ;-):



    It doesn't work as is on windows, but I've used it as a base on linux
    several times.

    HTH,

    --
    Emile van Sebille
    emile@fenx.com



    Comment

    • John Roth

      #3
      Re: Simultaneous output parsing of child process?


      "Fraser Gordon" <fraser.gordon@ cognos.com> wrote in message
      news:deb65cae.0 310020645.3af9c f62@posting.goo gle.com...[color=blue]
      > Hello,
      >
      > Hopefully someone can help me out with this issue...
      >
      > I have a python script that needs to run a shell command and be able
      > to react to output from that command as it occurs (as opposed to
      > waiting for the whole command to complete and then parsing the
      > output). Is this possible with Python?
      >
      > I created a test script that prints a line of output to stdout every 4
      > seconds or so. I want to create a "parent" script that kicks off the
      > test script, and prints data as soon as the test script sends anything
      > to the standard output (ie. every 4 seconds).
      >
      > Furthermore, the solution needs to support Windows, several flavors of
      > UNIX and potentially LINUX.
      >
      > I would ideally like to have the following type of behaviour:
      >
      > childPipe = kickOffChild('t est.py')
      > while <child process still active>:
      > line = childPipe.check ForStdOutput()
      > if line: # This should trigger once every 4 seconds in the
      > example above
      > print "OUTPUT:", line
      > print "Process completed."
      >
      >
      > Any help would be greatly appreciated.[/color]

      You need to use separate threads to process stdin, stdout and
      stderr. Otherwise, you'll run the risk of a deadlock.

      Unfortunately, the select service does not work on Windows
      in all circumstances, so you can't simply switch between the
      different files as needed.

      John Roth


      [color=blue]
      >
      > Fraser[/color]


      Comment

      Working...