发布消息
# -*- coding: utf-8 -*- import redis redis_con = redis.Redis(host='127.0.0.1') redis_con.publish(channel='test', message='hi-1')
订阅消息
# -*- coding: utf-8 -*- import redis redis_con = redis.Redis(host='127.0.0.1') pubsub = redis_con.pubsub() pubsub.subscribe('test') for item in pubsub.listen(): print(item)
接收到的消息
# 阻塞监听,第一条是测试数据 {'type': 'subscribe', 'pattern': None, 'channel': b'test', 'data': 1} {'type': 'message', 'pattern': None, 'channel': b'test', 'data': b'hi'}