标签:消息订阅 efault 消息 ESS init listen 序列化 patch 发布
import redis
from functools import wraps
class Subscribe:
def __init__(self, channel: str, **kwargs):
self.coon = redis.StrictRedis(**kwargs)
self.channel = channel
self.registerd = list()
self.course = self.coon.pubsub()
self.course.subscribe(self.channel)
def register(self, func):
self.registerd.append(func)
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
def listen(self):
messages = self.course.listen()
for message in messages:
self.dispatch(message)
def dispatch(self, message):
for handler in self.registerd:
handler(message)
DEFAULT_CHANNEL = 'channle'
subscribe = Subscribe(DEFAULT_CHANNEL)
@subscribe.register
def print_args(message):
print(message)
subscribe.listen()
import redis
coon = redis.StrictRedis()
coon.publish('channel', '发布的消息对象')
发布端只要向同一个数据库的里执行使用相同的key执行`publish`命令即可
标签:消息订阅 efault 消息 ESS init listen 序列化 patch 发布
原文地址:https://www.cnblogs.com/ivy-blogs/p/12505671.html