Python Thread Execution Issue . . .

Greetings!

I set up a basic threading specimen which does the job:

#!/usr/bin/python

import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()

Simple, but effective :wink:

Now, taking this basic construct and adding GTK bookends into the mix for guibuilding,

#!/usr/bin/python

import gtk
import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()
    
gtk.main()

...the otherwise healthy code hangs in class b at threading.Timer with the following effects:

  • Timer runs through its wait period (5 sec.); and halts short of calling tDone.
  • If CTRL + C is invoked before Timer has completed its wait, the residual timing will be executed normally; and tDone will be called at the correct interval.

Here's what this looks like from the terminal:

thread a finished
^CTraceback (most recent call last):
  File "foo", line 37, in <module>
    gtk.main()
KeyboardInterrupt
thread b finished

So, I guess the question is: How can GTK be incorporated into the preceding example without causing a thread hangup involving Timer?

Thanks again --

---------- Post updated at 01:22 PM ---------- Previous update was at 11:20 AM ----------

Fixed. It was a GTK exit issue to unravel:

#!/usr/bin/python

import gtk
import gobject
import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")
        gobject.timeout_add_seconds(4, gtk.main_quit)


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()

gtk.main()

All OK --

Cheers!