green polling to wait for notify
"A.M." <agentm-/iWpWt6iY7eAP89PaY/[email protected]> Mon, 23 Aug 2010 15:44:31 -0400
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
Hello,
I am using the great new coroutine feature to integrate eventlet with psycopg2. I wrote the following code to run a greenlet thread which merely waits for a NOTIFY event.
def postgresql_listen(connect_string):
dbconn = psycopg2.connect(connect_string)
#issue listen for events
dbconn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
dbcursor = dbconn.cursor()
dbcursor.execute('LISTEN test;');
dbcursor.close()
while True:
print 'poll in'
state = dbconn.poll()
print 'poll out'
if state == psycopg2.extensions.POLL_OK:
while dbconn.notifies:
notification = dbconn.notifies.pop()
print notification
select.select([dbconn.fileno()], [], []) #delete this line to cause infinite loop- I had to add this line to prevent it
elif state == psycopg2.extensions.POLL_READ:
select.select([dbconn.fileno()], [], [])
elif state == psycopg2.extensions.POLL_WRITE:
select.select([], [dbconn.fileno()], [])
else:
raise OperationalError("bad state from poll: %s" % state)
I am mimicking the documentation's approach to asynchronous polling- why does poll() always return POLL_OK in this case?
Is there a cleaner way to simply block for asynchronous events?
Cheers,
M