[pygtk] Need help with spawn_async and callback

Tony Freeman t0ny.fr33man at gmail.com
Tue Jul 27 21:23:22 WST 2010


Thanks Robert,

It took a few days, but I finally figured out how to go about it.  I needed
to first create a 'channel' to the cstdout file descriptor intiger.  Here's
what I did initially to get thing working:

import pygtk
import gtk
import gtk.glade
import gobject
import os
import io

keep_pulsing = True
textview = wTree.get_widget('textview1')
textbuffer=textview.get_buffer()

def cstdout_callback(fd, condition, channel):
	global keep_pulsing
	if condition == gobject.IO_HUP:
		keep_pulsing=False
	elif condition == gobject.IO_IN:
		text = channel.readline()
		iter = textbuffer.get_end_iter()
		textbuffer.insert(iter, text)
		textview.scroll_to_mark(textbuffer.get_insert(),0)
	return keep_pulsing

def update_progress_callback():
	global keep_pulsing
	if keep_pulsing:
		progressbar.pulse()
	else:
		write_status("Done")
		window2.hide()
	return keep_pulsing

def run_command(command):
	global keep_pulsing
	keep_pulsing=True
	textbuffer.set_text("")
	(cpid, cstdin, cstdout, cstderr) =
gobject.spawn_async(command,flags=gobject.SPAWN_DO_NOT_REAP_CHILD,standard_output=True)
	channel = io.open(cstdout)
	gobject.io_add_watch(cstdout, gobject.IO_HUP|gobject.IO_IN,
cstdout_callback, channel)
	gobject.timeout_add(150, update_progress_callback)
	window2.show()


Then when I went to work I found that on my RHEL5 system I could not import
io ... so after a while I discovered I could use fdopen to create the
channel.  All is well now ;-)

I have things documented here:
http://www.linuxquestions.org/questions/programming-9/pygtk-send-output-from-a-file-descriptor-to-a-textview-821595/#post4045253

-- Tony


On Mon, Jul 26, 2010 at 8:02 PM, Robert Schroll <rschroll at gmail.com> wrote:

> On 07/25/2010 08:46 PM, Tony Freeman wrote:
>
>> In short: What do I need to do in order to get the text being generated
>> by my child process to show up in the textview?
>>
>
> This doesn't answer your question, but have you considered running the
> child inside a vte.Terminal?  Faced with a similar problem, I created a
> window containing a vte.Terminal and then used vte.Terminal.fork_command()
> [1] to launch the child process.  The terminal will emit the 'child-exited'
> signal when the child exits (how convenient!), at which point I hide the
> window.  Hooking up the progress bar might be more difficult in this case,
> though.
>
> Good luck,
> Robert
>
> [1] As near as I can figure, fork_command() takes two arguments - the first
> is the name of the command to run and the second is a list of arguments.
>  But the first element of this list must be the name of the command to run.
>  I'm sure a UNIX guru could explain why this is the only obvious way to do
> things.
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.daa.com.au/pipermail/pygtk/attachments/20100727/b0ff5a47/attachment.html>


More information about the pygtk mailing list