Skip to content

Instantly share code, notes, and snippets.

@reorx
Created June 24, 2018 06:21
Show Gist options
  • Select an option

  • Save reorx/98c03f64b1eb0875af0bf28a4b7502db to your computer and use it in GitHub Desktop.

Select an option

Save reorx/98c03f64b1eb0875af0bf28a4b7502db to your computer and use it in GitHub Desktop.
# coding: utf-8
import gevent.monkey
gevent.monkey.patch_all()
import os
import redis
import gevent
REDIS_URL = os.environ['REDIS_URL']
rds = redis.StrictRedis.from_url(REDIS_URL)
psobj = rds.pubsub()
channel = 'channel:1'
def do_pub():
rds.publish(channel, 'a string')
def do_sub():
psobj.subscribe(channel)
for i in psobj.listen():
# on connect: {'pattern': None, 'type': 'subscribe', 'channel': 'channel:1', 'data': 1L}
# on new message: {'pattern': None, 'type': 'message', 'channel': 'channel:1', 'data': 'a string'}
print('get msg', i, type(i['data']), repr(i['data']))
gevent.sleep(0)
print('waiting')
gevent.spawn(do_sub)
while True:
gevent.sleep(3)
do_pub()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment