I am writing a program by a framework using pygtk. The main program doing the following things:
- Create a watchdog thread to monitor some resource
- Create a client to receive data from socket
- call
gobject.Mainloop()
but it seems after my program enter the Mainloop, the watchdog thread also won't run.
My workaround is to use gobject.timeout_add
to run the monitor thing.
But why does creating another thread not work?
Here is my code:
import gobject
import time
from threading import Threadclass MonitorThread(Thread):def __init__(self):Thread.__init__(self)def run(self):print "Watchdog running..."time.sleep(10)def main():mainloop = gobject.MainLoop(is_running=True)def quit():mainloop.quit()def sigterm_cb():gobject.idle_add(quit)t = MonitorThread()t.start()print "Enter mainloop..."while mainloop.is_running():try:mainloop.run()except KeyboardInterrupt:quit()if __name__ == '__main__':main()
The program output only "Watchdog running...Enter mainloop..", then nothing. Seems thread never run after entering mainloop.