Created
January 11, 2014 19:00
-
-
Save squeaky-pl/8375219 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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