Skip to content

Instantly share code, notes, and snippets.

@brainsik
Created November 14, 2013 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainsik/7475329 to your computer and use it in GitHub Desktop.
Save brainsik/7475329 to your computer and use it in GitHub Desktop.
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