Skip to content

Instantly share code, notes, and snippets.

@tudoanh
Forked from sunboy-2050/gist:9543963
Created December 12, 2021 06:02
Show Gist options
  • Save tudoanh/64b9d9f65fbd6a3890ebd79418136e11 to your computer and use it in GitHub Desktop.
Save tudoanh/64b9d9f65fbd6a3890ebd79418136e11 to your computer and use it in GitHub Desktop.
python implement redis publish and subscribe
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# http://blog.ithomer.net
import time
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channels):
threading.Thread.__init__(self)
self.redis = r
self.pubsub = self.redis.pubsub() # 发布订阅对象
self.pubsub.subscribe(channels) # 订阅
def work(self, item):
print item['channel'], ":", item['data']
def run(self):
for item in self.pubsub.listen():
print item
if item['data'] == "KILL":
print self, "unsubscribed and finished"
self.pubsub.unsubscribe() # 取消订阅
break
else:
self.work(item)
time.sleep(3) # 暂停1秒
if __name__ == "__main__":
r = redis.Redis()
client = Listener(r, ['test', 'test2']) # 仅订阅 'test', 'test2', 'fail' 将被过滤掉
client.start()
r.publish('test', 'this will reach the listener')
r.publish('test2', 'this will work')
r.publish('fail', 'this will not') # 被过滤掉了
r.publish('test', 'KILL')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment