码迷,mamicode.com
首页 > 其他好文 > 详细

redis实现消息队列

时间:2016-10-12 22:12:07      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

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

 

redis实现消息队列

标签:

原文地址:http://www.cnblogs.com/taiyang-li/p/5954218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!