Created
September 29, 2012 18:30
-
-
Save thomasboyt/3804817 to your computer and use it in GitHub Desktop.
redis pub/sub example
This file contains 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
import redis | |
r = redis.StrictRedis(host='localhost', port=6379, db=0) | |
# [... flask routing code, sql queries, etc. ...] | |
r.publish("sms_replies", "%s %s %s" % (user.nick, user.last_sms_sender, text)) |
This file contains 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
import redis | |
def redis_thread(conn): | |
r = redis.StrictRedis(host="localhost", port=6379, db=0) | |
r = r.pubsub() | |
r.subscribe("sms_replies") | |
while True: | |
for m in r.listen(): | |
if m['type'] == "message": | |
from_nick, to_nick, reply_body = m['data'].split(" ", 2) | |
conn.msg(to_nick, "SMS reply from %s: %s [Please do not attempt to reply to this message.]" % (from_nick, reply_body)) | |
# This hooks into an "on connection to the IRC server" event, so that the | |
# Redis thread is created once the bot connects to the server | |
@hook.event('004') | |
def irc_onconnect(paraml, conn=None): | |
t = threading.Thread(target=redis_thread, args=(conn,)) | |
t.setDaemon(True) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment