Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Created September 29, 2012 18:30
Show Gist options
  • Save thomasboyt/3804817 to your computer and use it in GitHub Desktop.
Save thomasboyt/3804817 to your computer and use it in GitHub Desktop.
redis pub/sub example
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))
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