[pygtk] Asynchronous access at output of os.popen() call

A.T.Hofkamp a.t.hofkamp at tue.nl
Wed Aug 20 18:31:36 WST 2008


Pádraig Brady wrote:
> togix wrote:
>> Hi,
>> I'm working on a GUI in PyGTK that allows to insert some parameters and then to call a program (written in C) using the os.popen() function.
>>
>> The thing that I want to do is redirect the standard output of the called program into a gtk.TextView() box [b]in runtime[/b],
>> so to show the lines when they come out in a asynchronous way, not all the end of execution.
> 
> Yes there are 2 issues here.
> The first is that os.popen() doesn't return until the subprocess is finished.

This is not true at my Linux platform:
-----------
import time, os

print "Just started, time is now", time.asctime()
fp = os.popen('sleep 4', 'r')
print "Opened, time is now", time.asctime()
fp.close()
print "Finished, time is now", time.asctime()
-----------
produces

Just started, time is now Wed Aug 20 12:26:51 2008
Opened, time is now Wed Aug 20 12:26:51 2008
Finished, time is now Wed Aug 20 12:26:55 2008

ie os.popen returns immediately, and fp.close() waits until the child has died 
(it returns the exit-status of the child iirc).

> To address the first issue, and read from the subprocess asynchronously
> you could try my subprocess module, with a timeout:
> http://www.pixelbeat.org/libs/subProcess.py

or for Python 2.4 and higher, use the standard subprocess module.


Albert



More information about the pygtk mailing list