def get_connection_pool(self): if not self.connection_pool: self.connection_pool = tornadis.ClientPool( host=self.options['host'], port=self.options['port'], password=self.options['password'], db=self.options['db_no'], max_size=self.options['max_connections']) return self.connection_pool
def get_redis_pool(host=None, port=None, uds=None): global redis_pools if uds is None: key = "%s:%i" % (host, port) else: key = uds if key not in redis_pools: kwargs = {"autoclose": True, "connect_timeout": options.timeout, "client_timeout": REDIS_POOL_CLIENT_TIMEOUT, "aggressive_write": True} if uds is None: kwargs["host"] = host kwargs["port"] = port kwargs["tcp_nodelay"] = True else: kwargs["unix_domain_socket"] = uds redis_pools[key] = tornadis.ClientPool(**kwargs) return redis_pools[key]
def __init__(self, host, port, selected_db=0, max_connections=4, client_timeout=7200, bulk_size=5): self.ioloop = IOLoop.current() self.max_connections = max_connections self.current_connections = 0 self.bulk_size = bulk_size self.pool = tornadis.ClientPool( max_size=-1, client_timeout=client_timeout, autoclose=True, host=host, port=port, db=selected_db, ) self._commands = [] self.executing = False
from tornado.ioloop import IOLoop from tornado.web import RequestHandler, Application, url import tornado.gen import tornadis import logging logging.basicConfig(level=logging.WARNING) POOL = tornadis.ClientPool(max_size=15, client_timeout=1, autoclose=True) class HelloHandler(RequestHandler): @tornado.gen.coroutine def get(self): with (yield POOL.connected_client()) as client: print POOL.get_connected_client() reply = yield client.call("PING") if not isinstance(reply, tornadis.TornadisException): self.write("Hello, %s" % reply) self.finish() def on_finish(self): print "finish" def make_app(): return Application([ url(r"/", HelloHandler), ]) def main():
from tornado.ioloop import IOLoop from tornado.web import RequestHandler, Application, url import tornado.gen import tornadis import logging logging.basicConfig(level=logging.WARNING) POOL = tornadis.ClientPool(max_size=15) class HelloHandler(RequestHandler): @tornado.gen.coroutine def get(self): with (yield POOL.connected_client()) as client: reply = yield client.call("PING") if not isinstance(reply, tornadis.TornadisException): self.write("Hello, %s" % reply) self.finish() def make_app(): return Application([ url(r"/", HelloHandler), ]) def main(): app = make_app() app.listen(8888) IOLoop.current().start()
#!/usr/bin/env python # coding=utf-8 ''' desc: redis相关的方法 author: jsongo ''' import tornadis from tornado import gen pool = 10 # host = '127.0.0.1' host = 'redis-server' port = 6379 pwd = '' POOL = tornadis.ClientPool(max_size=pool, host=host, port=port, autoconnect=True) getRedis = POOL.connected_client @gen.coroutine def execRedis(*args, **kwargs): result = None with (yield getRedis()) as redis: if not isinstance(redis, tornadis.TornadisException): data = kwargs.get('data') if args[0] == 'hmset' and data: params = [] [params.extend(x) for x in data.items()] tmpList = list(args)
""" Redis async client usage example https://github.com/thefab/tornadis/blob/master/examples/web.py """ # import time from itertools import chain import tornadis from tornado import gen from settings import REDIS_HOST, REDIS_PORT, REDIS_POOL_SIZE # from tornado.ioloop import IOLoop # client = tornadis.Client(host="localhost", port=6379, autoconnect=True) REDIS_POOL = tornadis.ClientPool( max_size=REDIS_POOL_SIZE, host=REDIS_HOST, port=REDIS_PORT, autoconnect=True ) # @gen.coroutine # def async_sleep(seconds=1): # yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds) @gen.coroutine def redis_ping() -> str: with (yield REDIS_POOL.connected_client()) as client: reply = yield client.call('PING') return str(reply, 'utf-8')
import tornado import tornadis @tornado.gen.coroutine def ping_redis(num): with (yield pool.connected_client()) as client: if not isinstance(client, tornadis.TornadisException): # client is a connected tornadis.Client instance # it will be automatically released to the pool thanks to the # "with" keyword reply = yield client.call("PING") if not isinstance(reply, tornadis.TornadisException): print("reply #%i : %s" % (num, reply)) @tornado.gen.coroutine def multiple_ping_redis(): yield [ping_redis(i) for i in range(0, 100)] pool = tornadis.ClientPool(max_size=5) loop = tornado.ioloop.IOLoop.instance() loop.run_sync(multiple_ping_redis) pool.destroy()