首先在使用过程中发现这个 tornado-redis 是有很多bug的,请参见之前的文章 https://blog.526net.com/?p=3229
连接池拿连接的时候,如果不存在他会连默认端口。
#coding:utf-8
import tornadoredis
import tornado.httpserver
import tornado.web
import tornado.ioloop
import tornado.gen
#设置连接池
CONNECTION_POOL = tornadoredis.ConnectionPool(max_connections=500,wait_for_available=True)
# 生成一个客户端,这里并没有给出host和port,使用的默认的参数,我的redis使用默认的参数也可以连接上
# Client的参数为
# def __init__(self, host=’localhost’, port=6379, unix_socket_path=None,
# password=None, selected_db=None, io_loop=None,
# connection_pool=None):
c = tornadoredis.Client(connection_pool=CONNECTION_POOL)
class MainHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
name = yield tornado.gen.Task(c.get,’name’)
age = yield tornado.gen.Task(c.get,’age’)
height = yield tornado.gen.Task(c.get,’height’)
self.finish("name: {0} age: {1} height: {2}".format(name, age, height))
application = tornado.web.Application([
(r’/’, MainHandler),
])
@tornado.gen.engine
def create_test_data():
c = tornadoredis.Client()
# 使用管道设置值
with c.pipeline() as pipe:
pipe.set(‘name’, ‘zhangsan’)
pipe.set(‘age’, ’10’)
pipe.set(‘height’, ‘1.8’)
yield tornado.gen.Task(pipe.execute)
if __name__ == ‘__main__’:
# Start the data initialization routine
create_test_data()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
print ‘Demo is runing at 0.0.0.0:8888\nQuit the demo with CONTROL-C’
tornado.ioloop.IOLoop.instance().start()