Created
January 17, 2016 20:32
-
-
Save Spindel/4b758dc14fc231e8e9fe to your computer and use it in GitHub Desktop.
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
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