redis实现消息队列

#!/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    

 

posted @ 2016-10-12 19:52  后端技术小屋  阅读(664)  评论(0编辑  收藏  举报