Forked from igniteflow/redis-producer-consumer.py
Last active
November 18, 2021 07:26
-
-
Save adamshamsudeen/ef17de58b78ee2e2f158dce5cf6e1966 to your computer and use it in GitHub Desktop.
A very simple implementation of a Redis producer-consumer messaging queue in Python3
This file contains 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
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(f'Consuming: {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