#NEWLINE# def run( self ) : #NEWLINE# #INDENT# "Execute events until the queue is empty.\n\n        When there is a positive delay until the first event, the\n        delay function is called and the event is left in the queue;\n        otherwise, the event is removed from the queue and executed\n        (its action function is called, passing it the argument).  If\n        the delay function returns prematurely, it is simply\n        restarted.\n\n        It is legal for both the delay function and the action\n        function to modify the queue or to raise an exception;\n        exceptions are not caught but the scheduler's state remains\n        well-defined so run() may be called again.\n\n        A questionable hack is added to allow other threads to run:\n        just after an event is executed, a delay of 0 is executed, to\n        avoid monopolizing the CPU when other threads are also\n        runnable.\n\n        " #NEWLINE# q = self . _queue #NEWLINE# delayfunc = self . delayfunc #NEWLINE# timefunc = self . timefunc #NEWLINE# pop = heapq . heappop #NEWLINE# while q : #NEWLINE# #INDENT# ( time , priority , action , argument ) = checked_event = q [ 0 ] #NEWLINE# now = timefunc ( ) #NEWLINE# if ( now < time ) : #NEWLINE# #INDENT# delayfunc ( ( time - now ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# event = pop ( q ) #NEWLINE# if ( event is checked_event ) : #NEWLINE# #INDENT# action ( * argument ) #NEWLINE# delayfunc ( 0 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# heapq . heappush ( q , event )