as processesthe efficiency and shared-state model of threads make them ideal for this role moreoversince most gui toolkits do not allow multiple threads to update the gui in parallelupdates are best restricted to the main thread because only the main thread should generally update the displaygui programs typically take the form of main gui thread and one or more long-running producer threads--one for each long-running task being performed to synchronize their points of interfaceall of the threads share data on global queuenon-gui threads post resultsand the gui thread consumes them more specificallythe main thread handles all gui updates and runs timer-based loop that wakes up periodically to check for new data on the queue to be displayed on-screen in python' tkinter toolkitfor instancethe widget after(msecsfunc*argsmethod can be used to schedule queue-check events because such events are dispatched by the gui' event processorall gui updates occur only in this main thread (and often mustdue to the lack of thread safety in gui toolkitsthe child threads don' do anything gui-related they just produce data and put it on the queue to be picked up by the main thread alternativelychild threads can place callback function on the queueto be picked up and run by the main thread it' not generally sufficient to simply pass in gui update callback function from the main thread to the child thread and run it from therethe function in shared memory will still be executed in the child threadand potentially in parallel with other threads since threads are much more responsive than timer event loop in the guithis scheme both avoids blocking the gui (producer threads run in parallel with the gui)and avoids missing incoming events (producer threads run independent of the gui event loop and as fast as they canthe main gui thread will display the queued results as quickly as it canin the context of slower gui event loop also keep in mind that regardless of the thread safety of gui toolkitthreaded gui programs must still adhere to the principles of threaded programs in general--access to shared resources may still need to be synchronized if it falls outside the scope of the producer/consumer shared queue model if spawned threads might also update another shared state that is used by the main gui threadthread locks may also be required to avoid operation overlap for instancespawned threads that download and cache email probably cannot overlap with others that use or update the same cache that isqueues may not be enoughunless you can restrict threadswork to queuing their resultsthreaded guis still must address concurrent updates we'll see how the threaded gui model can be realized in code later in this book for more on this subjectsee especially the discussion of threaded tkinter guis in threads