<s> """<STR_LIT>""" <EOL> import logging <EOL> import Queue <EOL> import threading <EOL> import time <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class ReQueue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> queue_capacity , <EOL> requeue_capacity = None , <EOL> queue_factory = Queue . Queue , <EOL> get_time = time . time ) : <EOL> """<STR_LIT>""" <EOL> if requeue_capacity is None : <EOL> requeue_capacity = queue_capacity <EOL> self . get_time = get_time <EOL> self . queue = queue_factory ( queue_capacity ) <EOL> self . requeue = queue_factory ( requeue_capacity ) <EOL> self . lock = threading . Lock ( ) <EOL> self . put_cond = threading . Condition ( self . lock ) <EOL> self . get_cond = threading . Condition ( self . lock ) <EOL> def _DoWithTimeout ( self , <EOL> action , <EOL> exc , <EOL> wait_cond , <EOL> done_cond , <EOL> lock , <EOL> timeout = None , <EOL> block = True ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None and timeout < <NUM_LIT:0.0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not block : <EOL> timeout = <NUM_LIT:0.0> <EOL> result = None <EOL> success = False <EOL> start_time = self . get_time ( ) <EOL> lock . acquire ( ) <EOL> try : <EOL> while not success : <EOL> try : <EOL> result = action ( ) <EOL> success = True <EOL> except Exception , e : <EOL> if not isinstance ( e , exc ) : <EOL> raise e <EOL> if timeout is not None : <EOL> elapsed_time = self . get_time ( ) - start_time <EOL> timeout -= elapsed_time <EOL> if timeout <= <NUM_LIT:0.0> : <EOL> raise e <EOL> wait_cond . wait ( timeout ) <EOL> finally : <EOL> if success : <EOL> done_cond . notify ( ) <EOL> lock . release ( ) <EOL> return result <EOL> def put ( self , item , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def PutAction ( ) : <EOL> self . queue . put ( item , block = False ) <EOL> self . _DoWithTimeout ( PutAction , <EOL> Queue . Full , <EOL> self . get_cond , <EOL> self . put_cond , <EOL> self . lock , <EOL> timeout = timeout , <EOL> block = block ) <EOL> def reput ( self , item , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def ReputAction ( ) : <EOL> self . requeue . put ( item , block = False ) <EOL> self . _DoWithTimeout ( ReputAction , <EOL> Queue . Full , <EOL> self . get_cond , <EOL> self . put_cond , <EOL> self . lock , <EOL> timeout = timeout , <EOL> block = block ) <EOL> def get ( self , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> def GetAction ( ) : <EOL> try : <EOL> result = self . requeue . get ( block = False ) <EOL> self . requeue . task_done ( ) <EOL> except Queue . Empty : <EOL> result = self . queue . get ( block = False ) <EOL> return result <EOL> return self . _DoWithTimeout ( GetAction , <EOL> Queue . Empty , <EOL> self . put_cond , <EOL> self . get_cond , <EOL> self . lock , <EOL> timeout = timeout , <EOL> block = block ) <EOL> def join ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . join ( ) <EOL> def task_done ( self ) : <EOL> """<STR_LIT>""" <EOL> self . queue . task_done ( ) <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . queue . empty ( ) and self . requeue . empty ( ) <EOL> def get_nowait ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( block = False ) <EOL> def qsize ( self ) : <EOL> return self . queue . qsize ( ) + self . requeue . qsize ( ) </s>
