I am writing a framework program using pygtk. The main program performs the following actions:
- Create a watchdog stream to monitor some resource
- Create a client to receive data from a socket
- call
gobject.Mainloop()
but it seems that after my program enters Mainloop, the control chain will not work either.
My workaround is to use gobject.timeout_add to start the monitor.
But why doesn't creating another thread work?
Here is my code:
import gobject import time from threading import Thread class 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 displays only "Watchdog running ... Enter mainloop ..", then nothing. It seems that the thread never starts after entering mainloop.
python multithreading pygtk
David guan
source share