Skip to content

Instantly share code, notes, and snippets.

@niwinz
Forked from fcurella/psql.py
Created August 28, 2012 07:22
Show Gist options
  • Save niwinz/3495817 to your computer and use it in GitHub Desktop.
Save niwinz/3495817 to your computer and use it in GitHub Desktop.
postgres PubSub vs Redis
#!/usr/bin/env python
import select
import time
import psycopg2
import psycopg2.extensions
import sys
def get_cursor():
conn = psycopg2.connect("dbname=pgpubsub")
curs = conn.cursor()
return curs, conn
def publish(curs):
for i in range(3):
t = time.time()
for j in xrange(99999):
curs.execute("NOTIFY test, '%s';" % 'hello')
curs.execute('COMMIT;')
delta_t = time.time() - t
print delta_t
def listen(curs, conn):
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
curs.execute("LISTEN test;")
print "Waiting for notifications on channel 'test'"
if select.select([conn], [], [], 5) == ([], [], []):
print "Timeout"
else:
while 1:
conn.poll()
while conn.notifies:
notify = conn.notifies.pop()
print "Got NOTIFY:", notify.pid, notify.channel, notify.payload
if __name__ == '__main__':
cursor, conn = get_cursor()
if sys.argv[1] == 'p':
publish(cursor)
else:
listen(cursor, conn)
#!/usr/bin/env python
import sys
import redis
import time
def redis_connection():
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
return r
def publish(conn):
for i in range(3):
t = time.time()
for j in xrange(99999):
conn.publish('test', 'hello')
delta_t = time.time() - t
print delta_t
def listen(conn):
r = conn.pubsub()
r.subscribe('test')
for m in r.listen():
print "Got message: %s" % m
if __name__ == '__main__':
conn = redis_connection()
if sys.argv[1] == 'p':
publish(conn)
else:
listen(conn)
| 1 listener | 2 listeners
------|---------------|---------------
| 17.6403880119 | 25.9326701164
psql | 18.8840239048 | 27.1259140968
| 17.6730439663 | 26.722645998
------|---------------|---------------
| 15.1958539486 | 18.6388731003
redis | 15.6854000092 | 18.7926390171
| 15.1056058407 | 19.2768361568
Note: the publisher and all listeners were running on the same machine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment