Created
January 3, 2014 23:14
-
-
Save sweenzor/8248572 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 random | |
import signal | |
import gevent | |
import redis | |
REDIS_URL = 'redis://127.0.0.1:6379' | |
REDIS_CHANNEL = 'pubsub_demo' | |
redis = redis.from_url(REDIS_URL) | |
def publisher(pid): | |
gevent.sleep(random.randint(0, 5) * 0.001) | |
redis.publish(REDIS_CHANNEL, 'message from publisher %d' % pid) | |
def subscriber(): | |
pubsub = redis.pubsub() | |
pubsub.subscribe(REDIS_CHANNEL) | |
for message in pubsub.listen(): | |
gevent.sleep(0.1) | |
if message['type'] == 'message': | |
print message['data'] | |
def main(): | |
tasks = [] | |
tasks.append(gevent.spawn(subscriber)) | |
tasks.extend([gevent.spawn(publisher, i) for i in xrange(20)]) | |
gevent.joinall(tasks) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment