发布订阅 消息多播,一个发布消息可以同时被多个订阅者收听 常用命令 发布 PUBLISH channel message 订阅 SUBSCRIBE channel [channel ...] PSUBSCRIBE pattern [pattern ...] python demo #!/usr/bin/env python import redis import time redis_pool = redis.ConnectionPool(host="192.168.6.14",port="6379") redis_client = redis.Redis(connection_pool=redis_pool) #生产者 def publishMessages(): while True: redis_client.publish("channel.shenyang","hello news "+time.asctime(time.localtime(time.time()))) time.sleep(2) #消费者 def subscibeMessages(): p = redis_client.pubsub() p.subscribe("channel.shenyang") while True: message = p.get_message() if message: print(message) else: time.sleep(1) ##加入超时时间堵塞读 def subscibeMessages(): p = redis_client.pubsub() p.subscribe("channel.shenyang") while True: message = p.get_message(timeout=10) # 10秒等待 if message: print(message) #阻塞消费者 def subscibeBlockMessages(): p = redis_client.pubsub() p.subscribe("channel.shenyang") for item in p.listen(): print(item['type'])……
阅读全文