class Subscriber(object): def __init__(self, channel_id): self.channel_id = channel_id self.r = RedisPubSub(host='127.0.0.1', port=6379, callback=self.callback) self.r.connect() def callback(self, evtype, *args, **kwargs): if evtype == CALLBACK_TYPE_CONNECTED: print 'connected' self.r.subscribe(self.channel_id) elif evtype == CALLBACK_TYPE_SUBSCRIBED: print 'subscribed to channel_id %s' % args[0] elif evtype == CALLBACK_TYPE_MESSAGE: print 'received on channel_id %s message %s' % (args[0], args[1]) self.r.unsubscribe() elif evtype == CALLBACK_TYPE_UNSUBSCRIBED: print 'unsubscribed' self.r.disconnect() elif evtype == CALLBACK_TYPE_DISCONNECTED: print 'disconnected' ioloop.stop()
def __init__(self, channel_id): self.channel_id = channel_id self.r = RedisPubSub(host='127.0.0.1', port=6379, callback=self.callback) self.r.connect()
import sys from async_pubsub.redis_pubsub import RedisPubSub if __name__ == '__main__': channel_id = sys.argv[1] message = sys.argv[2] RedisPubSub.publish(channel_id, message)