<s> import time <EOL> import redis <EOL> import umsgpack <EOL> from six . moves import queue as BaseQueue <EOL> class RedisQueue ( object ) : <EOL> """<STR_LIT>""" <EOL> Empty = BaseQueue . Empty <EOL> Full = BaseQueue . Full <EOL> max_timeout = <NUM_LIT> <EOL> def __init__ ( self , name , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , db = <NUM_LIT:0> , <EOL> maxsize = <NUM_LIT:0> , lazy_limit = True , password = None ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . redis = redis . StrictRedis ( host = host , port = port , db = db , password = password ) <EOL> self . maxsize = maxsize <EOL> self . lazy_limit = lazy_limit <EOL> self . last_qsize = <NUM_LIT:0> <EOL> def qsize ( self ) : <EOL> self . last_qsize = self . redis . llen ( self . name ) <EOL> return self . last_qsize <EOL> def empty ( self ) : <EOL> if self . qsize ( ) == <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> return False <EOL> def full ( self ) : <EOL> if self . maxsize and self . qsize ( ) >= self . maxsize : <EOL> return True <EOL> else : <EOL> return False <EOL> def put_nowait ( self , obj ) : <EOL> if self . lazy_limit and self . last_qsize < self . maxsize : <EOL> pass <EOL> elif self . full ( ) : <EOL> raise self . Full <EOL> self . last_qsize = self . redis . rpush ( self . name , umsgpack . packb ( obj ) ) <EOL> return True <EOL> def put ( self , obj , block = True , timeout = None ) : <EOL> if not block : <EOL> return self . put_nowait ( ) <EOL> start_time = time . time ( ) <EOL> while True : <EOL> try : <EOL> return self . put_nowait ( obj ) <EOL> except self . Full : <EOL> if timeout : <EOL> lasted = time . time ( ) - start_time <EOL> if timeout > lasted : <EOL> time . sleep ( min ( self . max_timeout , timeout - lasted ) ) <EOL> else : <EOL> raise <EOL> else : <EOL> time . sleep ( self . max_timeout ) <EOL> def get_nowait ( self ) : <EOL> ret = self . redis . lpop ( self . name ) <EOL> if ret is None : <EOL> raise self . Empty <EOL> return umsgpack . unpackb ( ret ) <EOL> def get ( self , block = True , timeout = None ) : <EOL> if not block : <EOL> return self . get_nowait ( ) <EOL> start_time = time . time ( ) <EOL> while True : <EOL> try : <EOL> return self . get_nowait ( ) <EOL> except self . Empty : <EOL> if timeout : <EOL> lasted = time . time ( ) - start_time <EOL> if timeout > lasted : <EOL> time . sleep ( min ( self . max_timeout , timeout - lasted ) ) <EOL> else : <EOL> raise <EOL> else : <EOL> time . sleep ( self . max_timeout ) <EOL> Queue = RedisQueue </s>
