import psycopg2 from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT import eventlet from eventlet import wsgi from eventlet import websocket from eventlet.hubs import trampoline dsn = "dbname=test" # customise this def dblisten(q): """ Open a db connection and add notifications to *q*. """ cnn = psycopg2.connect(dsn) cnn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) cur = cnn.cursor() cur.execute("LISTEN data;") while 1: trampoline(cnn, read=True) cnn.poll() while cnn.notifies: n = cnn.notifies.pop() q.put(n) @websocket.WebSocketWSGI def handle(ws): """ Receive a connection and send it database notifications. """ q = eventlet.Queue() eventlet.spawn(dblisten, q) while 1: n = q.get() print n ws.send(n.payload) def dispatch(environ, start_response): if environ['PATH_INFO'] == '/data': return handle(environ, start_response) else: start_response('200 OK', [('content-type', 'text/html')]) return [page] page = """ pushdemo
 
 
 
""" if __name__ == "__main__": listener = eventlet.listen(('127.0.0.1', 7000)) wsgi.server(listener, dispatch)