Skip to content

Instantly share code, notes, and snippets.

@vigo vigo/ forked from joymax/
Created Mar 13, 2018

What would you like to do?
class PubSub(object):
Very simple Pub/Sub pattern wrapper
using simplified Redis Pub/Sub functionality.
Usage (publisher)::
import redis
r = redis.Redis()
q = PubSub(r, "channel")
q.publish("test data")
Usage (listener)::
import redis
r = redis.Redis()
q = PubSub(r, "channel")
def handler(data):
print "Data received: %r" % data
def __init__(self, redis, channel="default"):
self.redis = redis = channel
def publish(self, data):
self.redis.publish(, json.dumps(data))
def subscribe(self, handler):
redis = self.redis.pubsub()
for data_raw in redis.listen():
if data_raw['type'] != "message":
data = json.loads(data_raw["data"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.