for in range( )time sleep( self dataqueue put('[producer id=%dcount=% ](idi)def consumer(self)trydata self dataqueue get(block=falseexcept queue emptypass elseself insert('end''consumer got =% \nstr(data)self see('end'self after( self consumer times per sec def makethreads(selfevent)for in range(self threadsperclick)threading thread(target=self producerargs=( ,)start(if __name__ ='__main__'root threadgui(root mainloop(in main threadmake guirun timer loop pop-up windowenter tk event loop watch for this threadtimer loopand shared queue technique to resurface later in this as well as in ' more realistic pyedit program example in pyeditwe'll use it to run external file searches in threadsso they avoid blocking the gui and may overlap in time we'll also revisit the classic producer/consumer thread queue model in more realistic scenario later in this as way to avoid blocking gui that must read an input stream--the output of another program thread exits in guis example - also uses python' threading module instead of _thread this would normally mean thatunlike the prior versionthe program would not exit if any producer threads are still runningunless they are made daemons manually by setting their daemon flag to true remember that under threadingprograms exit when only daemonic threads remainthe producer threads here inherit false daemon value from the thread that creates themwhich prevents program exit while they run howeverin this example the spawned threads finish too quickly to noticeably defer program exit change this script' time sleep call to seconds to simulate longerlived worker threads and witness this effect in action--closing the window after leftclick erases the windowbut the program itself then does not exit for roughly seconds ( its shell window is pausedif you do the same to the prior _thread versionor set this version' threadsdaemon flags to truethe program exits immediately instead in more realistic guisyou'll want to analyze exit policies in the context of running threadsand code accordinglyboth nondaemonic threading threads and thread locks in general can be used to defer exits if needed converselya perpetually running guisthreadsand queues