How to read stdout from subprocess as it is being produced

Alex alex.pulver at gmail.com
Sun Dec 21 16:52:02 CET 2008


On Dec 19, 5:09 pm, Albert Hopkins <mar... at letterboxes.org> 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



More information about the Python-list mailing list