Created
June 27, 2017 18:23
-
-
Save JirkaChadima/bec6ca1fc7ca3a98f3d12907cddaf4d5 to your computer and use it in GitHub Desktop.
listen-notify-database-handler
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
# http://initd.org/psycopg/docs/advanced.html#asynchronous-notifications | |
import select | |
import psycopg2 | |
import psycopg2.extensions | |
conn = psycopg2.connect(DSN) | |
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) | |
curs = conn.cursor() | |
cursor.execute("LISTEN field_value_change") | |
print "Waiting for notifications on channel 'test'" | |
while 1: | |
if select.select([conn],[],[],5) == ([],[],[]): | |
print "Timeout" | |
else: | |
conn.poll() | |
while conn.notifies: | |
notify = conn.notifies.pop(0) | |
print "Got NOTIFY:", notify.pid, notify.channel, notify.payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment