Pergunta

I'm using tornado and tornado-redis but I can't close the connection to redis without getting an error. Look at this example:

import tornadoredis    
import tornado.web    
import tornado.gen    

client = tornadoredis.Client()    
client.connect()    

class MainHandler(tornado.web.RequestHandler):    
    @tornado.web.asynchronous    
    @tornado.gen.engine    
    def get(self):    
        client.publish('test_channel', 'ahahah')    
        self.finish('Ok')    

class ListenerHandler(tornado.web.RequestHandler):    
    @tornado.web.asynchronous     
    @tornado.gen.engine     
    def get(self):    
        self.client = tornadoredis.Client()    
        self.client.connect()    
        yield tornado.gen.Task(self.client.subscribe, 'test_channel')    
        self.client.listen(self.from_redis)    

    def from_redis(self, msg):    
        print msg.kind               
        if msg.kind == 'message':         
            self.write(str(msg.body))    
            self.aaaa()                   
        if msg.kind == 'disconnect':     
            self.write('Redis error')    
            self.aaaa()                   

    def aaaa(self):                                     
        print('aaaa')                                   
        self.finish()
        if self.client.subscribed:                     
            self.client.unsubscribe('test_channel')                            
            self.client.disconnect()                                           

def main():                               
    app = tornado.web.Application([       
        (r"/", MainHandler),                    
        (r"/listen", ListenerHandler),          
    ])                                          
    app.listen(9898)                            
    tornado.ioloop.IOLoop.instance().start()    

if __name__ == "__main__":                                                                           
    main()

and test from command line with:

curl "http://yourip:9898/listen" &
curl "http://yourip:9898/"

All work fine except that the following error is returned when "self.client.disconnect()" is called:

Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1115, in _stack_context_handle_exception

raise_exc_info((type, value, traceback))

File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 302, in wrapped

ret = fn(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 550, in inner

self.set_result(key, result)

File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 476, in set_result

self.run()

File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 505, in run

yielded = self.gen.throw(*exc_info)

File "/usr/local/lib/python2.7/dist-packages/tornadoredis/client.py", line 1070, in listen

data = yield gen.Task(self.connection.readline)

File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 533, in run

self.yield_point.start(self)

File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 371, in start

self.func(*self.args, **self.kwargs)

File "/usr/local/lib/python2.7/dist-packages/tornadoredis/connection.py", line 154, in readline

raise ConnectionError('Tried to read from '

ConnectionError: Tried to read from non-existent connection

Do you have any suggestion to get the connection to redis closed in a clean and right way ?

Foi útil?

Solução

A good website for questions like that is https://volt.tech but you better get more specific, because when you ask it like that it will likely be closed as too broad.

Solução 2

A good website for questions like that is https://volt.tech but you better get more specific, because when you ask it like that it will likely be closed as too broad.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top