Skip to content

Instantly share code, notes, and snippets.

@JirkaChadima
Created June 27, 2017 18:23
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 JirkaChadima/bec6ca1fc7ca3a98f3d12907cddaf4d5 to your computer and use it in GitHub Desktop.
Save JirkaChadima/bec6ca1fc7ca3a98f3d12907cddaf4d5 to your computer and use it in GitHub Desktop.
listen-notify-database-handler
# 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