Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Nov 13 00:00:06 2016
  4.  
  5. @author: ergo
  6. """
  7.  
  8. import select
  9. import psycopg2
  10. import psycopg2.extensions
  11. import threading
  12.  
  13. def notify(conn):
  14. print "Waiting for notifications on channel 'cats'"
  15. while True:
  16. if select.select([conn],[],[],1) != ([],[],[]):
  17. conn.poll()
  18. while conn.notifies:
  19. notify = conn.notifies.pop(0)
  20. print "Got NOTIFY:", notify.pid, notify.channel, notify.payload
  21. print "Must refresh cache"
  22.  
  23. conn = psycopg2.connect("host=<host> dbname=<dbname> user=<user> password=<password>")
  24. conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
  25.  
  26. curs = conn.cursor()
  27. curs.execute("LISTEN cats;")
  28.  
  29. t = threading.Thread(target=notify, args=(conn,))
  30.  
  31. t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement