Created
November 14, 2013 22:21
-
-
Save brainsik/7475329 to your computer and use it in GitHub Desktop.
Monocle Twisted Redis subscriber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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