redis实现消息队列

  • Post author:
  • Post category:其他


#!/usr/local/bin/python                                                             
                                                                                    
import redis                                                                        
import time                                                                         
                                                                                    
class RedisQueue(object):                                                           
    def __init__(self, name, namespace, **redis_args):                              
        self.key = "%s:%s" % (namespace, name)                                      
        self.__db = redis.Redis(**redis_args)                                       
                                                                                    
    def qsize(self):                                                                
        return self.__db.llen(self.key)                                             
                                                                                    
    def empty(self):                                                                
        return self.qsize() == 0                                                    
                                                                                    
    def get(self, block=True, timeout=None):                                        
        if block:                                                                   
            item = self.__db.blpop(self.key, timeout=timeout)                       
        else:                                                                       
            item = self.__db.lpop(self.key)                                         
                                                                                    
        if item:                                                                    
            item = item[1]                                                          
        return item                                                                 
                                                                                    
    def put(self, item):                                                            
        self.__db.rpush(self.key, item)                                             
                                                                                    
    def get_nowait(self):                                                           
        return self.get(False)                                                      
                                                                                    
                                                                                    
r = RedisQueue('test', "liyang")                                                    
begin = time.clock()                                                                
for i in range(0, 100000):      
    r.put(str(i))                                                                
end = time.clock()                                                               
print end-begin                                                                  
                                                                                 
begin = time.clock()                                                             
for i in range(0, 100000):                                                       
    a = r.get()                                                                  
end = time.clock()                                                               
print end-begin    

转载于:https://www.cnblogs.com/taiyang-li/p/5954218.html