[pygtk] Threads in PyGTK: keep typing while ping-ing

Graham Whelan gawhelan at gmail.com
Tue Feb 17 23:39:29 WST 2009


2009/2/17 Mamahita Sela <mamahitasela at yahoo.com>:
> Hi Graham,
>
> Thank you very much. Your code, sure it helps.
>
> But, i still found that it only print (many many times) the ping result of first host (which is no reply host). Is this because i did not join all the threads?
>
> This is what i worry about threading. Should i join the threads manually?
>
> Please advise,
> M
>

Sorry, that's because of a typo in the code I posted. I indented the
return statement in the Main.do_ping() function too far. It should
have been:

def do_ping(self):
    for h in range(100, 120):
        host = '192.168.0.%d' %(h)
        #
        worker = PingHost(host, self.ping_result)
        worker.start()

    return True

Also, noting John's comment above, the PingHost.run() function would
be improved by writing it:

def run(self):
    result = self.host, commands.getstatusoutput('ping %s -c1' %(self.host))[0]
    gobject.idle_add(self.callback, result)

And the callback function should return False. This way the callback
will be executed in the gtk.main() loop which is probably what you
want.

Graham


More information about the pygtk mailing list