{"id":6033,"library":"pondpond","title":"Pond","description":"Pond is a high-performance object-pooling library for Python, designed to reduce memory usage and improve object borrow hit rates. It provides thread-safe and coroutine-safe mechanisms for managing a pool of objects, automatically recycling infrequently used instances. Currently at version 1.4.1, Pond maintains an active development and release cadence, with its latest update in February 2024.","status":"active","version":"1.4.1","language":"en","source_language":"en","source_url":"https://github.com/t-baby/pondpond","tags":["object pooling","performance","concurrency","thread-safe","coroutine-safe"],"install":[{"cmd":"pip install pondpond","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Used for approximate counting of object pool usage, replacing the 'Count-min sketch' in versions 1.3.0 and above.","package":"madoka"}],"imports":[{"symbol":"Pond","correct":"from pond import Pond"},{"note":"The PyPI package is 'pondpond', but the importable module is 'pond'.","wrong":"from pondpond import PooledObjectFactory","symbol":"PooledObjectFactory","correct":"from pond import PooledObjectFactory"}],"quickstart":{"code":"import time\nimport threading\nfrom pond import Pond, PooledObjectFactory\n\n# Define a factory for the objects to be pooled\nclass MyObject:\n    def __init__(self, name):\n        self.name = name\n        self.created_at = time.time()\n        self.is_active = True\n\n    def do_work(self):\n        return f\"Object {self.name} working. Active: {self.is_active}\"\n\n    def cleanup(self):\n        self.is_active = False\n        return f\"Object {self.name} cleaned up.\"\n\nclass MyObjectFactory(PooledObjectFactory):\n    def __init__(self, name_prefix):\n        self.name_prefix = name_prefix\n        self._counter = 0\n        self._lock = threading.Lock()\n\n    def create(self):\n        with self._lock:\n            self._counter += 1\n            obj = MyObject(f\"{self.name_prefix}-{self._counter}\")\n            print(f\"Created: {obj.name}\")\n            return obj\n\n    def destroy(self, obj):\n        print(f\"Destroyed: {obj.cleanup()}\")\n\n    def validate(self, obj):\n        # Example validation: object must be active\n        return obj.is_active\n\n    def activate(self, obj):\n        # Reset object state if necessary before borrowing\n        obj.is_active = True\n        print(f\"Activated: {obj.name}\")\n        return obj\n\n    def passivate(self, obj):\n        # Prepare object for recycling (e.g., clear transient data)\n        obj.is_active = False\n        print(f\"Passivated: {obj.name}\")\n\n# Instantiate the Pond with our factory\n# max_size: maximum number of objects to keep in the pool\n# time_between_eviction_runs: interval in seconds for cleaning up idle objects\nfactory = MyObjectFactory(\"worker\")\npool = Pond(factory, max_size=5, time_between_eviction_runs=5)\n\n# Borrow objects\nobj1 = pool.borrow()\nprint(obj1.do_work())\nobj2 = pool.borrow()\nprint(obj2.do_work())\n\n# Recycle objects\npool.recycle(obj1)\npool.recycle(obj2)\n\n# Borrow again (should get recycled objects first)\nobj3 = pool.borrow()\nprint(obj3.do_work())\n\n# Simulate some time for eviction to run (if time_between_eviction_runs > -1)\nprint(\"Waiting for potential eviction...\")\ntime.sleep(7)\n\n# Close the pool to destroy all remaining objects\npool.close()\nprint(\"Pool closed.\")\n","lang":"python","description":"This quickstart demonstrates how to set up an object pool using `pond`. It involves defining a `PooledObjectFactory` to manage the lifecycle (creation, destruction, validation, activation, passivation) of your custom objects. An instance of `Pond` is then created with this factory, allowing objects to be borrowed from and recycled back into the pool."},"warnings":[{"fix":"Always use `from pond import <Symbol>` for importing components of the library.","message":"The PyPI package name is `pondpond`, but the library's primary import is `from pond import ...`. Attempting `from pondpond import ...` will result in an `ImportError` for top-level classes like `Pond` or `PooledObjectFactory`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `madoka` is installed (`pip install madoka`). Review any custom code that might have deep integrations with Pond's internals for compatibility. Stick to the public API for object pooling operations.","message":"Starting from version 1.3.0, Pond replaced its internal `Queue` implementation with `deque` and `Count-min sketch` with the `madoka` library for significant performance improvements. While the public API for basic pooling operations (`borrow`, `recycle`) remains stable, any code that directly interacted with or relied on the specific internal data structures (e.g., inspecting the type of queue used) would be broken. This also introduces `madoka` as a new runtime dependency.","severity":"breaking","affected_versions":">=1.3.0"},{"fix":"This change is handled internally by the library. Users developing custom extensions or integrating Pond with older Python versions should be aware that daemon thread management might differ. It's recommended to use Python 3.8+ as specified by the library.","message":"In version 1.2.1, changes were made to align with Python's standard library updates. Specifically, `setDaemon()` for threads is deprecated in Python 3.9+, recommending the use of the `daemon` attribute directly instead. While this is a Python language change, it affects how `pondpond`'s internal threads are managed.","severity":"deprecated","affected_versions":">=1.2.1"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}