psycopg2 provides some example code for using postgresql notify facilities
import select
import psycopg2
import psycopg2.extensions
conn = psycopg2.connect(DSN)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()
curs.execute("LISTEN test;")
print "Waiting for notifications on channel 'test'"
while True:
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
What is the select.select([conn],[],[],5) == ([],[],[])
code doing?
The documentation of select.select
suggests that we are trying to make sure that the socket that underlies the conn
is "ready for reading".
What does it mean for a psycogp2 connection to be "ready for reading"?