Skip to content

Instantly share code, notes, and snippets.

@bamthomas
Created November 28, 2012 22:16

Revisions

  1. bamthomas revised this gist Nov 28, 2012. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions pub_sub_redis.py
    Original file line number Diff line number Diff line change
    @@ -13,12 +13,8 @@ def sub(myredis, name):
    for item in pubsub.listen():
    print '%s : %s' % (name, item['data'])


    if __name__ == '__main__':
    myredis = redis.Redis()
    write_p = Process(target=pub, args=(myredis,))
    write_p.start()
    read_p = Process(target=sub, args=(myredis,'reader1'))
    read_p.start()
    read_p2 = Process(target=sub, args=(myredis,'reader2'))
    read_p2.start()
    Process(target=pub, args=(myredis,)).start()
    Process(target=sub, args=(myredis,'reader1')).start()
    Process(target=sub, args=(myredis,'reader2')).start()
  2. bamthomas created this gist Nov 28, 2012.
    24 changes: 24 additions & 0 deletions pub_sub_redis.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    from multiprocessing.process import Process
    import time
    import redis

    def pub(myredis):
    for n in range(10):
    myredis.publish('channel','blah %d' % n)
    time.sleep(5)

    def sub(myredis, name):
    pubsub = myredis.pubsub()
    pubsub.subscribe(['channel'])
    for item in pubsub.listen():
    print '%s : %s' % (name, item['data'])


    if __name__ == '__main__':
    myredis = redis.Redis()
    write_p = Process(target=pub, args=(myredis,))
    write_p.start()
    read_p = Process(target=sub, args=(myredis,'reader1'))
    read_p.start()
    read_p2 = Process(target=sub, args=(myredis,'reader2'))
    read_p2.start()