[pygtk] Running a CLI program

Thomas Dybdahl Ahle lobais at gmail.com
Mon Feb 11 08:36:43 WST 2008


On Sun, 2008-02-10 at 23:13 +0100, Vláďa wrote:
> Hello,
> 
> I'm working on a multimedia converter. I plan to use CLI encoders (lame, 
> x264, xvid_encraw etc.)
> 
> But I don't know hot to run these programs effectively from PyGTK. I 
> need it to run on Windows at least.
> 
> I don't know hot to do following things:
> 1) Run a CLI application without displaying it's shell output (make it 
> hidden).
> 2) Get the encoder's text output in a 100ms interval to update 
> progressbar in my app.
> 
> I could also explain it as forwarding console output to a 
> variable/object. I hope it is clear what I'm trying to do.

You could use the python subprocess module.
However, when you are programming in pygtk I would very much suggest
that you look into gobject.spawn_async and gobject.io_add_watch.
http://www.pygtk.org/docs/pygobject/gobject-functions.html#function-gobject--spawn-async
http://www.pygtk.org/docs/pygobject/gobject-functions.html#function-gobject--io-add-watch
Those will make it easier for you to have your app responsive without all the trouble of using threads.

pid, stdin, stdout, stderr = gobject.spawn_async(["lame", "in.wav", "out.mp3"],
        standard_output=True, standard_error=True, flags=gobject.SPAWN_SEARCH_PATH)

def functionToCallWhenProcessExits (pid, condition):
    pass
gobject.child_watch_add(self.pid, functionToCallWhenProcessExits)

def onRead (fd, condition):
    pass
gobject.io_add_watch(stdout, gobject.IO_IN, onRead)
gobject.io_add_watch(stderr, gobject.IO_IN, onRead)

-- 
Best Regards,
Med Venlig Hilsen,
Thomas



More information about the pygtk mailing list