Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save seanieb/a5b5ee34cf3081a020ab0577c1b966b2 to your computer and use it in GitHub Desktop.
Save seanieb/a5b5ee34cf3081a020ab0577c1b966b2 to your computer and use it in GitHub Desktop.
A very simple implementation of a Redis producer-consumer messaging queue in Python
import redis
import time
import sys
def producer():
r = redis.Redis()
i = 0
while True:
r.rpush('queue', 'Message %d' % i)
i += 1
time.sleep(1)
def consumer():
r = redis.Redis()
while True:
val = r.blpop('queue')
print 'Consuming: (%s, %s)' % val
if __name__ == '__main__':
"""
Open up two terminals and run the two commands separately
"""
if sys.argv[1] == 'consumer':
print "Starting consumer: "
consumer()
else:
print "Starting producer: "
producer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment