redis pipeset發布訂閱
阿新 • • 發佈:2018-08-18
cti clas help 發布訂閱 author 分享 min while def
#!/usr/bin/env python # Author:Zhangmingda import redis,time pool = redis.ConnectionPool(host=‘192.168.11.5‘,port=6379,db=2) r = redis.Redis(connection_pool=pool) pipe = r.pipeline(transaction=True) pipe.set(‘age‘,‘22‘) time.sleep(15) pipe.execute() #如果不執行,則不會執行pipe.set 即不會真正存值pipeset
#!/usr/bin/env pythonredis_publish發布訂閱# Author:Zhangmingda #!/usr/bin/env python # Author:Zhangmingda import redis class RedisHelper: def __init__(self): self.__conn = redis.Redis(host=‘192.168.11.5‘,port=6379) self.chan_sub = ‘fm104.5‘ self.chan_pub = ‘fm104.5‘ def public(self, msg): #發布消息用函數 self.__conn.publish(self.chan_pub, msg) return True def subscribe(self): #接收消息用函數 pub = self.__conn.pubsub() pub.subscribe(self.chan_sub) pub.parse_response() return pub obj = RedisHelper() redis_sub = obj.subscribe() while True: msg = redis_sub.parse_response()print(msg)
#!/usr/bin/env python # Author:Zhangmingda import redis class RedisHelper: def __init__(self): self.__conn = redis.Redis(host=‘192.168.11.5‘,port=6379) self.chan_sub = ‘fm104.5‘ self.chan_pub = ‘fm104.5‘ def public(self, msg): #發布消息用函數 self.__conn.publish(self.chan_pub, msg) return True server = RedisHelper() server.public(‘hehe‘)redis發布消息
http://www.cnblogs.com/lianzhilei/p/5983673.html
redis pipeset發布訂閱