[pygtk] Threads in PyGTK: keep typing while ping-ing
Graham Whelan
gawhelan at gmail.com
Wed Feb 18 02:59:24 WST 2009
2009/2/17 Mamahita Sela <mamahitasela at yahoo.com>:
>
>> 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?
Having the callback return False is what prevents it from being called
again. Using gobject.idle_add() ensures that the callback is executed
from within the gtk.main() thread.
> 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.
>
Well, I'm not surprised the application won't quit normally. It's
creating 20 new threads every 5 seconds but only letting one thread
execute a ping request at a time. This is probably leading to an ever
increasing backlog of locked threads.
If you want to restrict it to one ping request at a time then you
should just create one thread and loop over all the requests from
within it, like Frédéric suggested above.
Graham
More information about the pygtk
mailing list