from gtornado.redis import RedisConnectionPool
from gtornado import green
from redis import StrictRedis
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, Application
from tornado.gen import coroutine

#green.enable_debug()
pool = RedisConnectionPool(host="localhost", port=18000, db=0)

class SubHandler(RequestHandler):

    def listen(self, channel):
        print("listen pub for channel %s" % channel)
        client = StrictRedis(connection_pool=pool)
        pubsub = client.pubsub()
        pubsub.subscribe(channel)
        for item in pubsub.listen():
            data = item["data"]
            if data != 1L:
                return data

    @coroutine
    def get(self):
        channel = self.get_argument("channel", "inbox")
        message = yield green.spawn(self.listen, channel, timeout=10)
        self.write(message)


class IfBlockingHandler(RequestHandler):
    @coroutine
    def get(self):
        self.write("no blocking")


class RedisTest(RequestHandler):
    def get_value(self):
        client = StrictRedis(connection_pool=pool)
        return client.get("name")

    @coroutine
    def get(self):
        name = yield green.spawn(self.get_value)
        self.write(name)

def redis_test(i):
    client = StrictRedis(host="localhost", db=0, connection_pool=pool)
    client.set("name_i" + str(i+1), "alex" + str(i))
    client.get("name")

def sleep():
    green.sleep(10)


app = Application([
                    (r"/sub", SubHandler),
                    (r"/name", RedisTest),
                    (r"/ifblocking", IfBlockingHandler),
                    ])

app.listen(30001)


#IOLoop.current().run_sync(start)    
IOLoop.instance().start()

@@ -374,61 +374,8 @@
 l):%0A
-        print(%22listen pub for channel %25s%22 %25 channel)%0A
     
@@ -454,16 +454,33 @@
 ubsub()%0A
+        try:%0A    
         
@@ -513,16 +513,20 @@
         
+  
+  
 for item
@@ -558,16 +558,20 @@
         
+    
 data = i
@@ -582,16 +582,20 @@
 %22data%22%5D%0A
+    
         
@@ -625,24 +625,28 @@
             
+    
 return data%0A
@@ -644,16 +644,94 @@
 urn data
+%0A        except:%0A            raise%0A        finally:%0A            pubsub.close()
 %0A%0A    @c

fix test_redis.py memory leaks