发布者pub.py
import redis conn = redis.Redis(host='127.0.0.1', decode_responses=True) conn.publish('gaoxin', '18')
订阅者sub.py
import redis conn = redis.Redis(host='127.0.0.1', decode_responses=True) # 生成一个订阅者对象 pubsub = conn.pubsub() # 订阅一个消息 pubsub.subscribe('gaoxin') # 创建一个接收 while True: print('working~~~') msg = pubsub.parse_response() print(msg)
PS: 运行多个订阅者,每个订阅者会等待接收发布者发送的消息,当发布者发布消息后,订阅者全部接收到