Skip to content

Repository files navigation

ProxyPatternPool

Generic Proxy and Pool classes for Python.

StatusTestsCoverageIssuesPythonVersionBadgesLicense

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()
    definit_app(config):
    db.set_obj(initializationfromconfig)
    # File "app.py"importdatabasefromdatabaseimportdb# db is a proxy to nothing# delayed initializationdatabase.init_app(config)
    # db is now a proxy to the initialized object

    When an internal pool is used, method _ret_objmust 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.

    importProxyPatternPoolasppp# start a pool with 2 resources created by "fun"pool=ppp.Pool(
    fun=lambdan: f"expensive object {n}",
    min_size=2, max_size=2, timeout=0.5,
    )
    # get resourcesa=pool.get(); b=pool.get() # max_size reachedtry:
    c=pool.get() # will timeout after 0.5 secondsassertFalseexceptppp.TimeOut:
    passpool.ret(a); pool.ret(b); # return resourcespool.shutdown()
    delpool

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.

About

Generic Proxy and Pool Classes for Python

Topics

Resources

Security policy

Stars

3 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages