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

Mamahita Sela mamahitasela at yahoo.com
Wed Feb 18 01:30:24 WST 2009


Dear Graham,

> 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
> 
Thank you very much :) Everything is worker properly. There is small delay, around 0.5 s, but that is OK. 

I need to learn more about threading :)


> 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)

This is because we prevent the callback to be called again? Is it why?

Following threading tutorial, i tried locking (assuming i do dangerous action, with ping only for example). So, i put new codes in PingHost class. 

class PingHost(threading.Thread):
    lock = threading.Lock()
    def __init__(self, host, callback):
        threading.Thread.__init__(self)
        self.host = host
        self.callback = callback

    def run(self):
        PingHost.lock.acquire()
        result = self.host, commands.getstatusoutput('ping %s -c1'%(self.host))[0]
        PingHost.lock.release()
        gobject.idle_add(self.callback, result)
        
The result is very strange. The application never ping more than 2 host and i never can quit the application normally. 

Please advise,
Thanks,
M.



      



More information about the pygtk mailing list