How to read stdout from subprocess as it is being produced

Discussion in 'Python' started by Alex, Dec 19, 2008.

  1. Alex

    Alex Guest

    Hi,

    I have a Pyhon GUI application that launches subprocess.
    I would like to read the subprocess' stdout as it is being produced
    (show it in GUI), without hanging the GUI.

    I guess threading will solve the no-hanging issue, but as far as I
    searched for now, I've only seen how to read the stdout after
    subprocess is finished.

    Thanks!
    Alex, Dec 19, 2008
    #1
    1. Advertising

  2. Alex

    Guest

    On Dec 19, 9:34 am, Alex <> wrote:
    > Hi,
    >
    > I have a Pyhon GUI application that launches subprocess.
    > I would like to read the subprocess' stdout as it is being produced
    > (show it in GUI), without hanging the GUI.
    >
    > I guess threading will solve the no-hanging issue, but as far as I
    > searched for now, I've only seen how to read the stdout after
    > subprocess is finished.
    >
    > Thanks!


    If I'm interpreting your needs correctly, then you may find this
    module helpful:

    http://code.activestate.com/recipes/440554/

    I've used it successfully in the past when I wanted to use native
    python code (no C extensions necessary) for asynchronous reading from
    a child process.

    I'm not sure if later versions of Python (2.6, 3.0) support this in
    the standard library. I haven't researched it.
    , Dec 19, 2008
    #2
    1. Advertising

  3. On Fri, 2008-12-19 at 06:34 -0800, Alex wrote:
    > Hi,
    >
    > I have a Pyhon GUI application that launches subprocess.
    > I would like to read the subprocess' stdout as it is being produced
    > (show it in GUI), without hanging the GUI.
    >
    > I guess threading will solve the no-hanging issue, but as far as I
    > searched for now, I've only seen how to read the stdout after
    > subprocess is finished.
    >


    I believe that's going to be highly dependent upon the particular, yet
    unspecified, GUI toolkit/API.

    There are probably a few ways. You're toolkit might native support for
    this, but one way would be to use a timer. Here is some pseudocode:

    class MyWindow(toolkit.Window):
    def __init__(self, ...):
    ...
    self.subprocess = subprocess.Popen(..., stdout=subprocess.PIPE)
    self.running = True

    ...
    toolkit.set_timeout(TIMEOUT_VAL, self.read_stdout)

    def read_stdout(self, ...):
    if not self.running:
    return
    char = self.subprocess.stdout.read(1)
    if char == '':
    self.running = False
    return
    self.update_something(char)
    toolkit.set_timeout(TIMEOUT_VAL, self.read_stdout)
    Albert Hopkins, Dec 19, 2008
    #3
  4. Alex

    Alex Guest

    On Dec 19, 5:09 pm, Albert Hopkins <> wrote:
    > On Fri, 2008-12-19 at 06:34 -0800, Alex wrote:
    > > Hi,

    >
    > > I have a Pyhon GUI application that launches subprocess.
    > > I would like to read the subprocess' stdout as it is being produced
    > > (show it in GUI), without hanging the GUI.

    >
    > > I guess threading will solve the no-hanging issue, but as far as I
    > > searched for now, I've only seen how to read the stdout after
    > > subprocess is finished.

    >
    > I believe that's going to be highly dependent upon the particular, yet
    > unspecified, GUI toolkit/API.
    >
    > There are probably a few ways. You're toolkit might native support for
    > this, but one way would be to use a timer.  Here is some pseudocode:
    >
    > class MyWindow(toolkit.Window):
    >     def __init__(self, ...):
    >         ...
    >         self.subprocess = subprocess.Popen(..., stdout=subprocess.PIPE)
    >         self.running = True
    >
    >         ...
    >         toolkit.set_timeout(TIMEOUT_VAL, self.read_stdout)
    >
    >     def read_stdout(self, ...):
    >         if not self.running:
    >             return
    >         char = self.subprocess.stdout.read(1)
    >         if char == '':
    >             self.running = False
    >             return
    >         self.update_something(char)
    >         toolkit.set_timeout(TIMEOUT_VAL, self.read_stdout)


    Hi,

    Thanks a lot for the tip!

    Alex
    Alex, Dec 21, 2008
    #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. Roman Neuhauser
    Replies:
    0
    Views:
    644
    Roman Neuhauser
    Apr 4, 2005
  2. robert
    Replies:
    4
    Views:
    544
    Stephane Chazelas
    Apr 21, 2006
  3. Christoph Haas
    Replies:
    0
    Views:
    377
    Christoph Haas
    Jun 13, 2006
  4. alf
    Replies:
    3
    Views:
    426
    Fredrik Lundh
    Oct 26, 2006
  5. hiral
    Replies:
    2
    Views:
    583
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page