for in range( )time sleep( print('put'dataqueue put('[producer id=%dcount=% ](idi)def consumer(root)tryprint('get'data dataqueue get(block=falseexcept queue emptypass elseroot insert('end''consumer got =% \nstr(data)root see('end'root after( lambdaconsumer(root) times per sec def makethreads()for in range( )_thread start_new_thread(producer( ,)if __name__ ='__main__'main gui threadspawn batch of worker threads on each mouse click from tkinter scrolledtext import scrolledtext root scrolledtext(root pack(root bind(''lambda eventmakethreads()consumer(rootstart queue check loop in main thread root mainloop(pop-up windowenter tk event loop observe how we fetch one queued data item per timer event here this is on purposealthough we could loop through all the data items queued on each timer eventthis might block the gui indefinitely in pathological cases where many items are queued quickly (imagine fast telemetry interface suddenly queueing hundreds or thousands of results all at onceprocessing one item at time ensures that the gui will return to its event loop to update the display and process new user inputs without becoming blocked the downside of this approach is that it may take awhile to work through very many items placed on the queue hybrid schemessuch as dispatching at most queued items per timer event callbackmight be useful in some such scenarioswe'll see an example like this later in this section (example - when this script is runthe main gui thread displays the data it grabs off the queue in the scrolledtext window captured in figure - new batch of four producer threads is started each time you left-click in the windowand threads issue "getand "putmessages to the standard output stream (which isn' synchronized in this example--printed messages might overlap occasionally on some platformsincluding windowsthe producer threads issue sleep calls to simulate long-running tasks such as downloading mailfetching query resultor waiting for input to show up on socket (more on sockets later in this left-clicked multiple times to encourage thread overlap in figure - guisthreadsand queues