evilkost / brukva

Asynchronous Redis client that works within Tornado IO loop.
Other
265 stars 33 forks source link

Subscribing twice on the same channel prevents further new subscriptions #24

Open lucap opened 12 years ago

lucap commented 12 years ago

The example code below should expose the issue. Subscribing twice on the same channel, prevents further new subscriptions on any channel.

Calling listen() only once doesn't seem to help.

import time
import random
from functools import partial

import brukva
import tornado

def start_data_producer():

    now = time.time()
    ioloop = tornado.ioloop.IOLoop.instance()

    c = brukva.Client()
    c.connect()

    def pub(channel):
        data = random.randint(0,10)
        print "Publishing %s on %s" % (data, channel)
        c.publish(channel, data)

    tick = tornado.ioloop.PeriodicCallback(partial(pub, "test"), 1000)
    tick.start()

    tick2 = tornado.ioloop.PeriodicCallback(partial(pub, "test_2"), 1000)
    tick2.start()

def run_test():

    now = time.time()
    ioloop = tornado.ioloop.IOLoop.instance()

    c = brukva.Client()
    c.connect()

    def callback(result):
        print "Received: %s" % result.body 

    def unsub(channel):
        print "=== Unsubscribing: %s ===" % channel
        c.unsubscribe(channel)

    def sub(channel):
        print "=== Suscribing: %s ===" % channel
        c.subscribe(channel)
        # Should listen() be called for every subscribe() call?
        c.listen(callback)    

    ioloop.add_timeout(now, partial(sub, "test")) 

    # If we don't perform this second subscribe call then all is good. 
    ioloop.add_timeout(now + 5, partial(sub, "test")) 

    ioloop.add_timeout(now + 10, partial(unsub, "test"))
    ioloop.add_timeout(now + 15, partial(sub, "test"))
    ioloop.add_timeout(now + 20, partial(sub, "test_2"))

if __name__ == '__main__':
    start_data_producer()    
    run_test()
    tornado.ioloop.IOLoop.instance().start()