def create_client_pool(): """ Function to create an object pool for ICAT client objects The ObjectPool class uses the singleton design pattern """ return ObjectPool( ICATClient, min_init=config.get_config_value( APIConfigOptions.CLIENT_POOL_INIT_SIZE), max_capacity=config.get_config_value( APIConfigOptions.CLIENT_POOL_MAX_SIZE), max_reusable=0, expires=0, )
def main(): pool = ObjectPool.get_instance() one = pool.get_resource() one.set_value(10) pool.set_resource(one) print "%s = %d" % (one, one.get_value()) one = pool.get_resource() one.set_value(100) pool.set_resource(one) print "%s = %d" % (one, one.get_value()) two = pool.get_resource() two.set_value(300) pool.set_resource(two) print "%s = %d" % (two, two.get_value())
def test_get_and_put_item_without_resetter(self): op = ObjectPool(_x_factory) x = op.get() uuid = x.uuid self.assertIsInstance(x, X) self.assertTrue(x.is_initialized) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4) x.use() self.assertFalse(x.is_initialized) op.put(x) self.assertEqual(op.num_available_objects, 1) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4) y = op.get() self.assertEqual(uuid, y.uuid) # y is what x was self.assertIsInstance(y, X) self.assertFalse(y.is_initialized) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4)
def test_pool_initialization_with_reset_and_limit(self): op = ObjectPool(construct=_x_factory, reset=_x_resetter, limit=10) self.assertEqual(op.num_objects, 0) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.max_num_objects, 10)
def test_pool_default_initialization(self): op = ObjectPool(_x_factory) self.assertEqual(op.num_objects, 0) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.max_num_objects, 4)
def setUp(self): self._op = ObjectPool(construct=_x_factory, reset=_x_resetter)
class TestObjectPool(unittest.TestCase): def setUp(self): self._op = ObjectPool(construct=_x_factory, reset=_x_resetter) def test_pool_default_initialization(self): op = ObjectPool(_x_factory) self.assertEqual(op.num_objects, 0) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.max_num_objects, 4) def test_pool_initialization_with_reset_and_limit(self): op = ObjectPool(construct=_x_factory, reset=_x_resetter, limit=10) self.assertEqual(op.num_objects, 0) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.max_num_objects, 10) def test_get_and_put_item_without_resetter(self): op = ObjectPool(_x_factory) x = op.get() uuid = x.uuid self.assertIsInstance(x, X) self.assertTrue(x.is_initialized) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4) x.use() self.assertFalse(x.is_initialized) op.put(x) self.assertEqual(op.num_available_objects, 1) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4) y = op.get() self.assertEqual(uuid, y.uuid) # y is what x was self.assertIsInstance(y, X) self.assertFalse(y.is_initialized) self.assertEqual(op.num_available_objects, 0) self.assertEqual(op.num_objects, 1) self.assertEqual(op.max_num_objects, 4) def test_get_and_put_item_with_resetter(self): x = self._op.get() uuid = x.uuid self.assertIsInstance(x, X) self.assertTrue(x.is_initialized) self.assertEqual(self._op.num_available_objects, 0) self.assertEqual(self._op.num_objects, 1) self.assertEqual(self._op.max_num_objects, 4) x.use() self.assertFalse(x.is_initialized) self._op.put(x) self.assertEqual(self._op.num_available_objects, 1) self.assertEqual(self._op.num_objects, 1) self.assertEqual(self._op.max_num_objects, 4) y = self._op.get() self.assertEqual(uuid, y.uuid) self.assertIsInstance(y, X) self.assertTrue(y.is_initialized) self.assertEqual(self._op.num_available_objects, 0) self.assertEqual(self._op.num_objects, 1) self.assertEqual(self._op.max_num_objects, 4) def test_get_max_num_objects(self): objects = [] for i in range(self._op.max_num_objects): x = self._op.get() self.assertEqual(self._op.num_objects, i + 1) self.assertEqual(self._op.num_available_objects, 0) self.assertEqual(self._op.max_num_objects, 4) objects.append(x) for i, x in enumerate(objects): self._op.put(x) self.assertEqual(self._op.num_objects, 4) self.assertEqual(self._op.num_available_objects, i + 1) self.assertEqual(self._op.max_num_objects, 4) def test_block_when_pool_is_empty(self): def work(pool, duration): x = pool.get() x.use(duration) pool.put(x) # Use threads to exhaust the pool and hold on to the resources # for 2 seconds each threads = [] for i in range(self._op.max_num_objects): t = Thread(target=work, args=(self._op, 2)) threads.append(t) t.start() # Busy wait to make sure every thread has acquired its resource while (self._op.num_available_objects != 0): continue # Use main thread to try to acquire a resource, and measure time # it has to wait (which is around 2 seconds) start = time.time() x = self._op.get() wait_time = time.time() - start self.assertGreater(wait_time, 1.5) self.assertLess(wait_time, 2.5) self._op.put(x) for t in threads: t.join() def test_get_object_using_context_manager(self): with self._op.item() as x: self.assertTrue(x.is_initialized) x.use() self.assertFalse(x.is_initialized) self.assertEqual(self._op.num_available_objects, 0) self.assertEqual(self._op.num_objects, 1) self.assertEqual(self._op.num_available_objects, 1) self.assertEqual(self._op.num_objects, 1) y = self._op.get() self.assertTrue(y.is_initialized) self._op.put(y) def test_work_simulation(self): def work(duration): with self._op.item() as x: x.use(duration) work_items = [uniform(0, 0.1) for i in range(100)] # create some contention by having more threads than resources tp = ThreadPool(self._op.max_num_objects + 2) tp.map(work, work_items) self.assertEqual(self._op.num_objects, 4) self.assertEqual(self._op.num_available_objects, 4) # ensure all objects are in good state objects = [] for i in range(self._op.max_num_objects): x = self._op.get() self.assertTrue(x.is_initialized) objects.append(x) self.assertEqual(self._op.num_available_objects, 0) self.assertEqual(len(objects), self._op.max_num_objects)
import pygame import math from rect import Rect from line import Line from point import Point from object_pool import ObjectPool rect_pool = ObjectPool(100, Rect, 0, 0, 0, 0) line_pool = ObjectPool(100, Line, 0, 0, 0, 0) point_pool = ObjectPool(100, Point, 0, 0) def rectangles_overlap(a, b): local_a = rect_pool(0, 0, 0, 0) local_b = rect_pool(0, 0, 0, 0) _copy_and_normalize_rect(a, local_a) _copy_and_normalize_rect(b, local_b) return (local_a.x <= (local_b.x + local_b.width) and local_b.x <= (local_a.x + local_a.width) and local_a.y <= (local_b.y + local_b.height) and local_b.y <= (local_a.y + local_a.height)) def point_on_line(point, line): result = False if ((line.x1 <= point.x and point.x <= line.x2) or (line.x2 <= point.x and point.x <= line.x1)):
import psycopg2 import config from object_pool import ObjectPool pools = dict() for db_name in config.config['databases']: # TODO create a pool object and add to pools dict pools[db_name] = ObjectPool(config.config['databases'][db_name]) def getPool(db_name): return pools[db_name]