Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Monocle Twisted Redis subscriber
# encoding: utf-8
import sys
import monocle
monocle.init("twisted")
from monocle import _o
from monocle.stack import eventloop
import txredis.client
from twisted.internet import reactor, protocol
from twisted.python import log
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
class RedisSubscriber(txredis.client.RedisSubscriber):
def messageReceived(self, channel, message):
"""
Called when this connection is subscribed to a channel that
has received a message published on it.
"""
log.msg("<{}> {}".format(channel, message))
def getRedisSubscriber():
clientCreator = protocol.ClientCreator(reactor, RedisSubscriber)
return clientCreator.connectTCP(REDIS_HOST, REDIS_PORT)
@_o
def run():
redis = yield getRedisSubscriber()
redis.subscribe("w00t")
log.msg("subscribed to w00t")
def main():
log.startLogging(sys.stdout)
eventloop.queue_task(0, run)
eventloop.run()
if __name__ == "__main__":
main()
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.