Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A short script exploring Redis pubsub functions in Python
import redis
import threading
class Listener(threading.Thread):
def __init__(self, r, channels):
self.redis = r
self.pubsub = self.redis.pubsub()
def work(self, item):
print item['channel'], ":", item['data']
def run(self):
for item in self.pubsub.listen():
if item['data'] == "KILL":
print self, "unsubscribed and finished"
if __name__ == "__main__":
r = redis.Redis()
client = Listener(r, ['test'])
r.publish('test', 'this will reach the listener')
r.publish('fail', 'this will not')
r.publish('test', 'KILL')

This comment has been minimized.

Copy link

@Deisss Deisss commented Aug 30, 2013

On some redis version, the client subscriber can't be the same as client publisher, which means:

r = redis.Redis()
client = Listener(redis.Redis(), ["test"])

Is a better version...


This comment has been minimized.

Copy link

@truthadjustr truthadjustr commented Jul 1, 2016

hi Delsss, what Redis version did you mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment