Created
May 13, 2018 14:06
-
-
Save davideuler/3bdda9300a1cebcd33764c9023c0dbda to your computer and use it in GitHub Desktop.
Publish and scribe redis message in python
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
from multiprocessing import Process | |
import time | |
import redis | |
# publish random message | |
def pub(myredis): | |
for n in range(10): | |
myredis.publish('channel_rand','blah %d' % n) | |
time.sleep(1) | |
# subscribe messages | |
def sub(myredis, name): | |
pubsub = myredis.pubsub() | |
pubsub.subscribe(['channel_rand']) | |
for item in pubsub.listen(): | |
if item['type'] == 'message': | |
print ('%s : %s' % (name, item['data'])) | |
if __name__ == '__main__': | |
myredis = redis.Redis() | |
Process(target=sub, args=(myredis,'reader1')).start() | |
Process(target=sub, args=(myredis,'reader2')).start() | |
Process(target=pub, args=(myredis,)).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment