<s> """<STR_LIT>""" <EOL> import Queue <EOL> import logging <EOL> import threading <EOL> import time <EOL> import traceback <EOL> import ufora . BackendGateway . ComputedGraph as ComputedGraph <EOL> queues = { } <EOL> nextFrameQueues = { } <EOL> queuesLock = threading . Lock ( ) <EOL> _no_tsunami_reload = True <EOL> def getDeferredUpdateQueue ( isNextFrameQueue = False ) : <EOL> graph = ComputedGraph . ComputedGraph . currentGraph ( ) <EOL> assert graph is not None , "<STR_LIT>" <EOL> queuesDict = queues if not isNextFrameQueue else nextFrameQueues <EOL> with queuesLock : <EOL> queue = queuesDict . get ( graph ) <EOL> if queue is None : <EOL> queue = Queue . Queue ( ) <EOL> queuesDict [ graph ] = queue <EOL> return queue <EOL> def makeDeferCallback ( func ) : <EOL> """<STR_LIT>""" <EOL> def newCallback ( * args ) : <EOL> def caller ( ) : <EOL> func ( * args ) <EOL> push ( caller ) <EOL> return newCallback <EOL> def push ( func ) : <EOL> getDeferredUpdateQueue ( ) . put ( func ) <EOL> def pushForNextFrame ( func ) : <EOL> getDeferredUpdateQueue ( isNextFrameQueue = True ) . put ( func ) <EOL> def moveNextFrameToCurFrame ( ) : <EOL> curQueue = getDeferredUpdateQueue ( ) <EOL> nextQueue = getDeferredUpdateQueue ( isNextFrameQueue = True ) <EOL> while True : <EOL> try : <EOL> curQueue . put ( nextQueue . get_nowait ( ) ) <EOL> except Queue . Empty : <EOL> break <EOL> def pullAll ( timeout = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> deferredUpdates = getDeferredUpdateQueue ( ) <EOL> while timeout is None or time . time ( ) < timeout : <EOL> updater = deferredUpdates . get_nowait ( ) <EOL> try : <EOL> updater ( ) <EOL> except : <EOL> logging . error ( traceback . format_exc ( ) ) <EOL> except Queue . Empty : <EOL> pass <EOL> def pullOne ( timeout = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> updater = getDeferredUpdateQueue ( ) . get ( True , timeout ) <EOL> try : <EOL> updater ( ) <EOL> except : <EOL> logging . error ( traceback . format_exc ( ) ) <EOL> return True <EOL> except Queue . Empty : <EOL> return False <EOL> def drainWithoutExecuting ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> deferredUpdates = getDeferredUpdateQueue ( ) <EOL> while True : <EOL> deferredUpdates . get_nowait ( ) <EOL> except Queue . Empty : <EOL> pass </s>
