def test_pool(self): pool = ResourcePool(10, self.get_resource) threads = [ gevent.spawn(self.random_worker(), pool) for _ in range(1000) ] gevent.joinall(threads) assert pool.alloc_count <= pool.max_count assert pool.used_count == 0 assert self.counter == 10
def credis_bench(): credis_pool = ResourcePool(10, Connection, host=HOST, port=PORT) credis_connection.execute("flushdb") start = time.time() for x in range(NUM_INSERTS): with credis_pool.ctx() as conn: conn.execute("hset", "words", "word|{}".format(x), "1") print('CRedis HSET Done. Duration=', time.time() - start) hset_dict['credis'] = time.time() - start start = time.time() for x in range(NUM_INSERTS): with credis_pool.ctx() as conn: conn.execute("hget", "words", "word|{}".format(x)) print('CRedis HGET Done. Duration=', time.time() - start) hget_dict['credis'] = time.time() - start
def __init__(self, ip, port, db=0, pwd=None): self.sip = ip self.sport = port self.sdb = db self.spwd = pwd if platform.system() == 'Linux': self.redis_pool = ResourcePool(MAX_POOL_SIZE, Connection, host=self.sip, port=self.sport, db=self.sdb, password=self.spwd) else: self.redis_pool = redis.ConnectionPool(host=self.sip, port=self.sport, db=self.sdb, password=self.spwd) self.ping()
def worker_using_with(pool): try: with pool.ctx() as res: gevent.sleep(0.05) raise TestException('bad worker1') except TestException: pass def worker_using_with2(pool): try: with pool.ctx() as res: gevent.sleep(0.05) raise TestException('bad worker2') except TestException: pass def random_worker(): return random.choice([ worker_normal, worker_exception, worker_using_with, worker_using_with2 ]) pool = ResourcePool(10, get_resource) threads = [gevent.spawn(random_worker(), pool) for i in range(1000)] gevent.joinall(threads) assert pool.alloc_count <= pool.max_count assert pool.used_count == 0 assert counter == 10
import time import redis from time import time import asyncio import asyncio_redis from aredis import StrictRedis import pandas as pd retry_attempts = 1 hashmap_sizes = [50000] HOST = '127.0.0.1' PORT = 6379 # Credis connection credis_pool = ResourcePool(10, Connection, host=HOST, port=PORT) #Aredis client connection aredis_client = StrictRedis(host=HOST, port=PORT, db=0) #AsyncIO redis connection asyncio_redis_connection = asyncio_redis.Pool.create(host=HOST, port=PORT, poolsize=10) # Golang extension connection cpipe.Connect(HOST, PORT) # Golang extension connection with golang lib cpipelib.Connect(HOST, PORT)
def create_pool(url): info = parse_redis_url(url) poolmax = int(info.pop('poolmax', 32)) return ResourcePool(poolmax, Connection, **info)