我使用龙卷风和龙卷风 - 但是我无法在没有错误的情况下关闭与redis的连接。看看这个例子:
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()
并从命令行测试:
curl "http://yourip:9898/listen" &
curl "http://yourip:9898/"
所有工作都正常,但是当" self.client.disconnect()"时会返回以下错误:被称为:
追踪(最近一次通话): 文件" /usr/local/lib/python2.7/dist-packages/tornado/web.py",第1115行,_stack_context_handle_exception
raise_exc_info((type,value,traceback))
文件" /usr/local/lib/python2.7/dist-packages/tornado/stack_context.py" ;,第302行,包裹
ret = fn(* args,** kwargs)
文件" /usr/local/lib/python2.7/dist-packages/tornado/gen.py" ;,第550行,内在
self.set_result(键,结果)
文件" /usr/local/lib/python2.7/dist-packages/tornado/gen.py",第476行,在set_result中
self.run()
文件" /usr/local/lib/python2.7/dist-packages/tornado/gen.py" ;,第505行,在运行中
yielded = self.gen.throw(* exc_info)
文件" /usr/local/lib/python2.7/dist-packages/tornadoredis/client.py" ;,第1070行,在听
data = yield gen.Task(self.connection.readline)
文件" /usr/local/lib/python2.7/dist-packages/tornado/gen.py" ;,第533行,在运行中
self.yield_point.start(个体)
File" /usr/local/lib/python2.7/dist-packages/tornado/gen.py" ;,第371行,开始
self.func(* self.args,** self.kwargs)
File" /usr/local/lib/python2.7/dist-packages/tornadoredis/connection.py" ;,第154行,在 readline的
引发ConnectionError('尝试阅读'
ConnectionError:尝试从不存在的连接中读取
您是否有任何建议以干净正确的方式关闭redis连接?