Brainstorm's snippets (1/233)

  Sinewave redis PUBSUB example

sinewave publisher

import math, time
import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0)

for n in range(0, 100000):
    row = (62 * 'X')[0:32 + int(30*math.sin(n/8.0))]
    r.publish('sinewave', row)
    #print(row)
    print('\x1b[1;36;40m' + row + '\x1b[0m')
    time.sleep(0.01)

sinewave listener

import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=True)

pubsub = r.pubsub()
pubsub.subscribe('sinewave')

for item in pubsub.listen():
    if item['type'] == 'message':
        print(item['data'])