Threads in PyGTK: keep typing while ping-ing

bieffe62 at gmail.com bieffe62 at gmail.com
Wed Feb 18 16:19:20 CET 2009


On 17 Feb, 02:53, Mamahita Sela <mamahitas... at yahoo.com> wrote:
> Dear FB,
>
> > As you have been already told, join() blocks until the
> > thread is
> > terminated. and you should avoid that.
> > A simple fix could by add a timeout to t.join, doing
> > something like :
> >         t.join(JOIN_TIMEOUT);
> >         if not t.isAlive():
> >             print t.result
>
> Thanks for your great sample. And, thank you for your tips on speedup the ping stuff.
>
> However, i still have problem when adding timeout to join().
> - It works, but only print out thread result that is not alive, as we put in the code. So, i never get result from unsuccessful ping action, which might take more time than timeout specified.
> - I still have little delay in GUI. Is it possible to make it smooth in GUI operation? When i put more timeout (say: 0.5 from 0.2), i will have to wait  longer.
>
> PS: thanks for pointing out inefficient ping action :) I will follow the tips. But, another time I might need to do something else, like downloading files, or checking for serial devices. So, this thread thing, imho, is very important.
>
> Any help would be very appreciated.
> M.


You could make all_threads an attribute of main instance, and use it
to keep track of thread still alive. Therefore your
do_ping code shoulds be somethiong like this (NOT TESTED):

       for h in range(100, 105):
            host = '192.168.0.%d' %(h)
            #
            worker = PingHost(host)
            worker.start()
            self.all_threads.append(worker)
        #
        for t in self.all_threads:
           t.join(JOIN_TIMEOUT);
           if not t.isAlive():
                print t.result
                self.all_threads.remove(t)

        #
        return True


In this way, you could use a small value for JOIN_TIMEOUT and make the
GUI more responsive. The thread which are not
found completed when do_ping is called will stay in self.all_threads
and will be tested next time.
I did not check the documentation, but assumed that
gobject.timeout_add installs a _periodic_ timer. If it is not
so, you have to reinstall the timer at the end of do_ping, if
self.all_threads is not empty.
And don't forget to initialize self.all_threads in __init__ ;)

HTH

Ciao
-----
FB








More information about the Python-list mailing list