Skip to main content

A generic object pool implementation

Project description

generic-pool

generic-pool is a generic object pool for python inspired by the node.js library.

You can use it to build your own resource pools, e.g. to manage file handles, connections or similar.

Install

Install generic-pool from pypi using your favourite package manager.

# If you use poetry
poetry add generic-pool

# If you use pip
pip install generic-pool

Usage

from random

from generic_pool import Factory
from generic_pool import Pool

class IntValue:
    def __init__(self):
        self.value = random.rand
    pass

class FortyTwoFactory(Factory):
    def create(self) -> IntValue:
        return IntValue()

    def validate(self, item: IntValue) -> bool:
        return item.value == 42

    def destroy(self):
        # free resources, not applicable here
        pass

factory = FortyTwoFactory()
pool = Pool(factory)

item = pool.acquire()
try:
    assert item.value == 42
finally:
    pool.release(item)

with pool.acquire(item):
    assert item.value == 42

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

generic-pool-0.1.2.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

generic_pool-0.1.2-py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page