Advertisement
Guest User

listen / notify

a guest
Jun 18th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. Output when connected to 9.2.8 without "problems":
  2. -------------------------------------------------
  3. LISTEN data_0
  4. LISTEN data_1
  5. LISTEN data_2
  6. LISTEN data_3
  7. LISTEN data_4
  8. LISTEN data_5
  9. LISTEN data_6
  10. LISTEN data_7
  11. LISTEN data_8
  12. LISTEN data_9
  13.  
  14. 1: POLL_OK
  15.  
  16. 2: POLL_OK
  17.  
  18. 3: POLL_OK
  19. [Notify(20218, 'data_2', ''), Notify(20218, 'data_3', ''), Notify(20218, 'data_1', '')]
  20. 3: data_1
  21. [Notify(20218, 'data_2', ''), Notify(20218, 'data_3', '')]
  22. 3: data_3
  23. [Notify(20218, 'data_2', '')]
  24. 3: data_2
  25.  
  26. 4: POLL_OK
  27.  
  28.  
  29. Output when connected to 9.2.8 with "problems":
  30. ---------------------------------------------
  31.  
  32. LISTEN data_0
  33. LISTEN data_1
  34. LISTEN data_2
  35. LISTEN data_3
  36. LISTEN data_4
  37. LISTEN data_5
  38. LISTEN data_6
  39. LISTEN data_7
  40. LISTEN data_8
  41. LISTEN data_9
  42. 1: POLL_OK
  43.  
  44. 2: POLL_OK
  45.  
  46. 3: POLL_OK
  47. [Notify(30642, 'data_2', '')]
  48. 3: data_2
  49.  
  50. 4: POLL_OK
  51. [Notify(30642, 'data_3', '')]
  52. 4: data_3
  53.  
  54. 5: POLL_OK
  55. [Notify(30642, 'data_1', '')]
  56. 5: data_1
  57.  
  58. 6: POLL_OK
  59.  
  60.  
  61. Python test script
  62. ------------------
  63.  
  64. #!/usr/bin/env python
  65. import psycopg2,time
  66. from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
  67.  
  68. dsn = "host=host dbname=database user=username"
  69.  
  70. cnn = psycopg2.connect(dsn)
  71. cnn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
  72. cur = cnn.cursor()
  73.  
  74. for x in range(0, 10):
  75. sql = "LISTEN data_" + str(x)
  76. cur.execute(sql)
  77. print sql
  78.  
  79. id=1
  80.  
  81. while 1:
  82.  
  83. while 1:
  84. state = cnn.poll()
  85. if state == psycopg2.extensions.POLL_OK:
  86. print str(id) + ": POLL_OK"
  87. break
  88. elif state == psycopg2.extensions.POLL_READ:
  89. select.select([conn.fileno()], [], [])
  90. print str(id) + ": POLL READ"
  91. else:
  92. raise psycopg2.OperationalError("poll() returned %s" % state)
  93.  
  94. while cnn.notifies:
  95. print str(id)
  96. print cnn.notifies
  97.  
  98. n = cnn.notifies.pop().channel
  99.  
  100. print str(id) + ': ' + n
  101. time.sleep(2)
  102.  
  103. print "\n"
  104. time.sleep(2)
  105. id = id +1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement