queued callbacks on each timer event may block gui indefinitelybut running only one can take long time or consume cpu for timer events ( progress)assumes callback is either short-lived or updates display as it runsafter callback runthe code here reschedules and returns to event loop and updatesbecause this perpetual loop runs in main threaddoes not stop program exit################################################################################def threadchecker(widgetdelaymsecs= perevent= )for in range(perevent)try(callbackargsthreadqueue get(block=falseexcept queue emptybreak elsecallback(*args /sec /timer pass to set speed run < callbacks anything readyrun callback here widget after(delaymsecsreset timer event lambdathreadchecker(widgetdelaymsecsperevent)back to event loop ################################################################################in new thread run actionmanage thread queue puts for exits and progressrun action with args nowlater run oncalls with contextcalls added to queue here are dispatched in main thread onlyto avoid parallel gui updatesallows action to be fully ignorant of use in thread hereavoids running callbacks in thread directlymay update gui in threadsince passed func in shared memory called in threadprogress callback just adds callback to queue with passed argsdon' update in-progress counters herenot finished till exit actions taken off queue and dispatched in main thread by threadchecker################################################################################def threaded(actionargscontextonexitonfailonprogress)tryif not onprogresswait for action in this thread action(*argsassume raises exception if fails elsedef progress(*any)threadqueue put((onprogressany context)action(progress=progress*argsexceptthreadqueue put((onfail(sys exc_info()context)elsethreadqueue put((onexitcontext)def startthread(actionargscontextonexitonfailonprogress=none)thread start_new_threadthreaded(actionargscontextonexitonfailonprogress)################################################################################ thread-safe counter or flaguseful to avoid operation overlap if threads update other shared state beyond that managed by the thread callback queue ################################################################################ gui coding techniques