Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Created January 11, 2014 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeaky-pl/8375219 to your computer and use it in GitHub Desktop.
Save squeaky-pl/8375219 to your computer and use it in GitHub Desktop.
# consumer
from __future__ import print_function
import gevent
from gevent.monkey import patch_all
import psycogreen.gevent
import psycopg2
patch_all()
psycogreen.gevent.patch_psycopg()
def do_some_listen():
connection = psycopg2.connect(
database='test', user='postgres', password='postgres',
host='localhost', port=5433)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
cursor.execute('LISTEN spam;')
while 1:
gevent.select.select([connection], [], [])
print('POSTGRES woke up')
connection.poll()
while connection.notifies:
notify = connection.notifies.pop()
print(notify)
gevent.spawn(do_some_listen)
# producer
from __future__ import print_function
import gevent
import gevent.monkey
import psycogreen.gevent
import psycopg2
#gevent.monkey.patch_all()
psycogreen.gevent.patch_psycopg()
def main():
connection = psycopg2.connect(
database='test', user='postgres', password='postgres',
host='localhost', port=5433)
connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
for x in range(10):
cursor.execute("SELECT pg_notify('spam', %s);", (str(x),))
print('Sent', x)
gevent.sleep(1)
job = gevent.spawn(main)
gevent.joinall([job])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment