Skip to main content

Generic Proxy and Pool Classes for Python

Project description

ProxyPatternPool

Generic Proxy and Pool classes for Python.

Status Tests Coverage Issues Python Version Badges License

This module provides two classes:

  • Proxy implements the proxy pattern, i.e. all calls to methods on the proxy are forwarded to an internally wrapped object. This allows to solve the classic chicken-and-egg importation and initialization possibly circular-dependency issue with Python modules:

    # File "database.py"
    db = Proxy()
    
    def init_app(config):
        db.set_obj(initialization from config)
    
    # File "app.py"
    import database
    from database import db  # db is a proxy to nothing
    
    # delayed initialization
    database.init_app(config)
    
    # db is now a proxy to the initialized object
    

    When an internal pool is used, method _ret_obj must be called to return the object to the pool when done with it.

  • Pool implements a full-featured thread-safe pool of things which can be used to store expensive-to-create objects such as database connections, to be shared between threads for instance. The above proxy object creates a pool automatically depending on its parameters.

    This generic pool class can be used independently of the Proxy class.

    It provides numerous hooks to provide callbacks for creation, deletion, stats, tracing, health check… which makes it ideal to manage any kind of expensive resources within a process.

    import ProxyPatternPool as ppp
    
    # start a pool with 2 resources created by "fun"
    pool = ppp.Pool(
        fun = lambda n: f"expensive object {n}",
        min_size=2, max_size=2, timeout=0.5,
    )
    
    # get resources
    a = pool.get(); b = pool.get()  # max_size reached
    try:
        c = pool.get()  # will timeout after 0.5 seconds
        assert False
    except ppp.TimeOut:
        pass
    
    pool.ret(a); pool.ret(b);  # return resources
    
    pool.shutdown()
    del pool
    

License

This code is Public Domain.

All software has bug, this is software, hence… Beware that you may lose your hairs or your friends because of it. If you like it, feel free to send a postcard to the author.

Versions

Sources, documentation and issues are hosted on GitHub. Install package from PyPI. See version details.

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

proxypatternpool-11.6.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

proxypatternpool-11.6-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file proxypatternpool-11.6.tar.gz.

File metadata

  • Download URL: proxypatternpool-11.6.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for proxypatternpool-11.6.tar.gz
Algorithm Hash digest
SHA256 099577535d616ec79e8fc1b896bfb2533fbe548aeaefea7d40f48ff2fbf4870f
MD5 f9b6c3640413c1970ced3e3ac560b30e
BLAKE2b-256 010f7f6d782d33c4c57469fb596114b27fc36a55336a4c78c9adaaa93d2ab456

See more details on using hashes here.

File details

Details for the file proxypatternpool-11.6-py3-none-any.whl.

File metadata

File hashes

Hashes for proxypatternpool-11.6-py3-none-any.whl
Algorithm Hash digest
SHA256 551e5a9529cc5afbcc6bbf1d480386ea23caaf0db6f72f264e167b9c22f859a6
MD5 a1d6af3e12827e09c0c21cd50888967f
BLAKE2b-256 1a5b37fbf857eda98c951b98e63740cf63e6033fcc41c88e83a21e3fc50652bf

See more details on using hashes here.

Supported by

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