Threads in PyGTK: keep typing while ping-ing

Mamahita Sela mamahitasela at
Mon Feb 16 08:47:22 EST 2009

Dear All,

I have read several howtos for threading in PyGTK. I have tried some but with no luck.

What i want is: i can keep typing in gtk.TextView while periodically doing ping some hosts.
I think, the thread part is working since i can ping so fast (even for not reply host, around 3 s for 27 no-reply-host). But, when the ping action is running, i can not typing in textview. What i type while pinging will show up after ping action is done. 

(I am using 2.5.1, pygtk 2.10.6 on Linux x86)

This is my code:
import threading
import commands 
import gtk
import gobject


class PingHost(threading.Thread):
    def __init__(self, host):
        threading.Thread.__init__(self) = host
        self.result = ()
    def run(self):
        self.result =, commands.getstatusoutput('ping %s -c1' %([0]

class Main:
    def __init__(self): = gtk.Window()'destroy', gtk.main_quit)
        self.textb = gtk.TextBuffer()
        self.textv = gtk.TextView(self.textb)
        self.textv.set_size_request(500, 500)
        gobject.timeout_add(5000, self.do_ping)

        def do_ping(self):
        all_threads = []
        for h in range(100, 105):
            host = '192.168.0.%d' %(h)
            worker = PingHost(host)
        for t in all_threads:
            print t.result
        return True

if __name__ == '__main__':
    app = Main()

Any help would be appreciated :)
Best regards,


More information about the Python-list mailing list