Commits

Andriy Kornatskyy  committed 7ee7c1d

Introduced pooling module with EagerPool and Pooled.

  • Participants
  • Parent commits fbbd696

Comments (0)

Files changed (4)

File doc/modules.rst

 .. automodule:: wheezy.core.luhn
    :members:
 
+wheezy.core.pooling
+-------------------
+
+.. automodule:: wheezy.core.pooling
+   :members:
+
 wheezy.core.url
 ---------------
 

File src/wheezy/core/comp.py

 
 
 if PY3:  # pragma: nocover
+    from queue import Queue
+    xrange = range
     str_type = str
 
     def ntob(n, encoding):
     u = lambda s: s
 
 else:  # pragma: nocover
+    from Queue import Queue
+    xrange = xrange
     str_type = unicode
 
     def ntob(n, encoding):

File src/wheezy/core/pooling.py

+
+""" ``pooling`` module.
+"""
+
+from wheezy.core.comp import Queue
+from wheezy.core.comp import xrange
+
+
+class EagerPool(object):
+    """ Eager pool implementation.
+
+        Allocates all pool items during initialization.
+    """
+
+    def __init__(self, create_factory, size):
+        self.size = size
+        items = Queue(size)
+        for i in xrange(size):
+            items.put(create_factory())
+        self.__items = items
+        self.acquire = items.get
+        self.get_back = items.put
+
+    @property
+    def count(self):
+        """ Returns a number of available items in the pool.
+        """
+        return self.__items.qsize()
+
+
+class Pooled(object):
+    """ ``Pooled`` serves context manager purpose, effectively acquiring and
+        returning item to the pool.
+
+        Here is an example::
+
+            with Pooled(pool) as item:
+                # do something with item
+    """
+    __slots__ = ('pool', 'item')
+
+    def __init__(self, pool):
+        self.pool = pool
+
+    def __enter__(self):
+        self.item = item = self.pool.acquire()
+        return item
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.pool.get_back(self.item)
+        self.item = None

File src/wheezy/core/tests/test_pooling.py

+
+""" Unit tests for ``wheezy.core.pooling``.
+"""
+
+import unittest
+
+
+class EagerPoolTestCase(unittest.TestCase):
+
+    def test_init(self):
+        """
+        """
+        from wheezy.core.pooling import EagerPool
+        pool = EagerPool(lambda: 1, 10)
+        assert pool.size == 10
+        assert pool.count == 10
+
+    def test_acquire(self):
+        from wheezy.core.pooling import EagerPool
+        pool = EagerPool(lambda: 1, 10)
+
+        assert 1 == pool.acquire()
+        assert pool.size == 10
+        assert pool.count == 9
+
+    def test_get_back(self):
+        from wheezy.core.pooling import EagerPool
+        pool = EagerPool(lambda: 1, 10)
+
+        item = pool.acquire()
+        pool.get_back(item)
+
+        assert pool.size == 10
+        assert pool.count == 10
+
+
+class PooledTestCase(unittest.TestCase):
+
+    def test_scope(self):
+        from wheezy.core.pooling import EagerPool
+        from wheezy.core.pooling import Pooled
+        pool = EagerPool(lambda: 1, 10)
+        pooled = Pooled(pool)
+
+        item = pooled.__enter__()
+        assert 1 == item
+        assert pool.count == 9
+        pooled.__exit__(None, None, None)
+        assert pool.count == 10