Skip to content

Instantly share code, notes, and snippets.

@Spindel
Created January 17, 2016 20:32
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 Spindel/4b758dc14fc231e8e9fe to your computer and use it in GitHub Desktop.
Save Spindel/4b758dc14fc231e8e9fe to your computer and use it in GitHub Desktop.
def listen_forever(handlers):
session = DBSession()
engine = session.get_bind()
conn = engine.connect()
del engine
conn.detach()
session.close()
from sqlalchemy import text
for channel in handlers.keys():
cmd = "LISTEN {};".format(channel)
theLog.info("Setting up listener for: %s", channel)
listen = text(cmd).execution_options(autocommit=True)
conn.execute(listen)
del text, channel, cmd, listen
while True:
if select.select([conn.connection], [], [], 1) == ([], [], []):
pass
else:
conn.connection.poll()
while conn.connection.notifies:
session = DBSession()
notification = conn.connection.notifies.pop()
theLog.debug("NOTIFY: pid:%s channel:%s payload:%s",
notification.pid, notification.channel,
notification.payload)
handler = handlers[notification.channel]
handler(session, notification.payload)
session.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment