Created
April 23, 2014 19:03
-
-
Save bufferx/11228374 to your computer and use it in GitHub Desktop.
toredis connection pool test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2014 Zhang ZY<http://github.com/bufferx> | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); you may | |
# not use this file except in compliance with the License. You may obtain | |
# a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
# License for the specific language governing permissions and limitations | |
# under the License. | |
from tornado.gen import engine as gen_engine | |
from tornado.gen import Task as gen_Task | |
from tornado.ioloop import IOLoop | |
from tornado.options import define, options, parse_command_line | |
from tornado.web import RequestHandler, Application, asynchronous | |
from toredis.pool import ClientPool as RedisClientPool | |
from toredis.client import Client as RedisClient | |
import signal | |
define("use_redis_pool", type=bool, default=True) | |
define("redis_pool_size", type=int, default=20) | |
define("redis_host", type=str, default='localhost') | |
define("redis_port", type=int, default=6379) | |
define("big_data_size", type=int, default=512*1024) | |
define("small_data_size", type=int, default=1024) | |
define("bind", type=int, default='0.0.0.0') | |
define("port", type=int, default=8000) | |
class TApplication(Application): | |
@property | |
def datas(self): | |
if not hasattr(self, '_datas'): | |
self._datas = 'a' * options.data_size | |
return self._datas | |
@property | |
def redis_client(self): | |
if not options.use_redis_pool: | |
if not hasattr(self, '_redis_client'): | |
self._redis_client = RedisClient() | |
self._redis_client.connect(options.redis_host, options.redis_port) | |
return self._redis_client | |
if not hasattr(self, '_redis_pool'): | |
self._redis_pool = RedisClientPool( | |
pool_size=options.redis_pool_size, | |
host=options.redis_host, | |
port=options.redis_port,) | |
return self._redis_pool.client | |
@gen_engine | |
def __init__(self, *args, **kwargs): | |
big_datas = '0' * options.big_data_size | |
small_datas = '1' * options.small_data_size | |
task_list = [] | |
for idx, datas in enumerate((big_datas, small_datas)): | |
key = '%d:%d' % (id(self), idx) | |
task_list.append(gen_Task(self.redis_client.set, key, datas)) | |
print key | |
responses = yield task_list | |
print responses | |
handlers = [ | |
("/", RootHandler), | |
("/redis/get/big", RedisGetHandler, {'_type': 0}), | |
("/redis/get/small", RedisGetHandler, {'_type': 1}), | |
] | |
kwargs['handlers'] = handlers | |
super(TApplication, self).__init__(*args, **kwargs) | |
class RootHandler(RequestHandler): | |
def get(self): | |
self.write("Hello, world") | |
def _log(self): | |
pass | |
class RedisHandler(RequestHandler): | |
@property | |
def redis_client(self): | |
return self.application.redis_client | |
def _response(self, chunk): | |
self.finish(chunk) | |
class RedisGetHandler(RedisHandler): | |
def initialize(self, _type='0'): | |
self._data_type = _type | |
@asynchronous | |
@gen_engine | |
def get(self): | |
key = '%d:%s' % (id(self.application), self._data_type) | |
self.redis_client.get(key, callback=self._response) | |
def handle_sigchld(sig, frame): | |
IOLoop.instance().add_callback(IOLoop.instance().stop) | |
def main(): | |
signal.signal(signal.SIGCHLD, handle_sigchld) | |
parse_command_line() | |
for key, option in options.iteritems(): | |
print 'Option: (%s, %s)' % (key, option.value()) | |
app = TApplication() | |
app.listen(options.port, address=options.bind) | |
IOLoop.instance().start() | |
IOLoop.instance().close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
SINGLE CONNECTION
CONNECTION POOL