Skip to content

Instantly share code, notes, and snippets.

@lanuma
Created August 5, 2020 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanuma/ee9b362adda740cd2d369452e5abb149 to your computer and use it in GitHub Desktop.
Save lanuma/ee9b362adda740cd2d369452e5abb149 to your computer and use it in GitHub Desktop.
Learn redsi pubsub with python
import redis
import time
alice_r = redis.Redis(db=0)
alice_r.pubsub()
i = 0
while True:
print(f"publishing {i}")
alice_r.publish('tespubsub', f'Data => {i}')
i = i+1
time.sleep(1)
import redis
from redis.client import Redis
import asyncio
import time
def subscribe():
bob_r = Redis(host='localhost', port=6379, db=0)
bob_p = bob_r.pubsub()
data = bob_p.subscribe('tespubsub')
while True:
message = bob_p.get_message()
if message:
print(f'Subscriber: {message["data"]}')
time.sleep(2)
if __name__ == '__main__':
subscribe()
# asyncio.get_event_loop().run_until_complete(subscribe())
# asyncio.get_event_loop().run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment