threads and Tkinter
Petr Klyushkin
petrk at pochtamt.ru
Wed Jul 24 16:48:55 EDT 2002
More information about the Python-list mailing list
Wed Jul 24 16:48:55 EDT 2002
- Previous message (by thread): threads and Tkinter
- Next message (by thread): Queue.Queue examples?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello, Alex! >>>>> "Alex" == Alex Martelli writes: Alex> Petr Klyushkin wrote: >> Hello again! >> >> I have one more question about Tkinter. This time it is related >> with threads. I have a program which uses Tkinter as its GUI >> toolkit. It runs a separate "worker" thread for doing some >> time-consuming operation. Sometimes worker thread has to update >> GUI. Alex> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 Alex> It's also in the printed Cookbook, with some minor Alex> enhancements, but you can get the gist from the online-posted Alex> version at this URL. Hmm, two persons already answered on this post here and it seems that none of them even looked below the first paragraph of it because it sounds like a FAQ. I already know about 'periodical queue check' solution. I just want to know whether Tkinter is thread-safe. It seemed to me that it is because of Tcl lock in _tkinter.c I have received an advise by e-mail to unite thread messages and Tk messages handling in one function by overriding mainloop() method in Tkinter.Tk derived class. Here is a code snippet (i've tested it and it works nice for now on Windows and Linux): def mainloop(self, *args): """similar to mainloop, but also checks our Queue""" while not self.__quitMainLoop: while not self.thr_msg_queue.empty(): # process message from thread dispatch = self.thr_msg_queue.get() apply(dispatch[0], dispatch[1:]) # Do one event. Don't block if no events available. # '2' means TCL_DONT_BLOCK: # tcl.h: #define TCL_DONT_WAIT (1<<1) result = self.tk.dooneevent(2) if result == 0: time.sleep(0.020) #sleep for 20ms to allow other threads run if result < 0: break self.thr_msg_queue is Queue.Queue instance used by worker threads to communicate with GUI thread. Messages are really ready-to-use arguments for apply() function: function object and arguments. Are there any caveats and disadvantages in this code comparing to usual 'polling' solution? Alex> Alex -- C'ya, Peter. --=[petrk at pochtamt.ru]=--=[http://petrk.pochtamt.ru]=-- --=[ICQ 89449080]=--=[Jabber dassburger at jabber.ru]=--
- Previous message (by thread): threads and Tkinter
- Next message (by thread): Queue.Queue examples?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list