Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

Summary

objectpool is a Python implementation for object pools. The library let's you

  • create object pools
  • put arbitrary objects into pools
  • subscribe for new objects

Possible use cases for objectpool are: communication between threads, object storage, object distribution

Installation

Currently there is no official release yet. However, you can build a python wheel by using
python3 -m build
This will create a wheel which can be installed by using pip.

Example


fromobjectpoolimportObjectPoolfromMyObjectimportMyObjectfromAnotherObjectimportAnotherObjectdefhandle_my_obj(obj):
print(obj)
if__name__=="__main__":
pool=ObjectPool("My Pool")
pool.register_pool("P1")
pool.subscribe("P1", MyObject, handle_my_obj)
pool.put("P1", AnotherObject())

API


Create new pools

pool=ObjectPool("Main Pool Name")
pool.register_pool("Pool Name 1")
pool.register_pool("Pool Name 2", max_size=100)
pool.register_pool("Pool Name 1", fail_if_already_registered=False)

Put objects into pool

Objects which can be put into a pool have to inherit from GenericObject:

fromobjectpoolimportGenericObjectclassMyObject(GenericObject):
_cnt=0def__init__(self, _cnt: int=None):
super().__init__()
self._cnt=_cntdefset_cnt(self, cnt: int):
self._cnt=cntdefget_cnt(self):
returnself._cntdef__str__(self):
returnf"MyObject(cnt={self._cnt})"
foriinrange(100):
obj=MyObject(i)
pool.put("P1", obj)

Subscribe for new objects

You can subscribe for any new object which is put into a pool:

defhandle_myobject(obj: MyObject):
print("Object MyObject was put into pool")
pool.subscribe("P1", MyObject, handle_myobject)

Subscriptions support arguments and filter functions:

defhandle_myobject(obj: MyObject, args):
print("Object MyObject was put into pool")
print(f"Args = {args})
deffilter_myobject(obj: MyObject, min_size: int):
returnobj.get_cnt() >=min_sizepool.subscribe("P1", MyObject, handle_myobject, args=["argument 1", 4321], filter_func=filter_myobject, filter_func_args=50)

If a filter function is provided, the function is called on every reception of the specified object. If (and only if) the filter function returns true, the handle function is called.

Miscellaneous

Deregister pool:

pool.deregister_pool("P1")
pool.deregister_pool("P2", fail_if_already_deregistered=False)

Get number of objects in pool:

pool.size("P1")

Get a list of all objects in pool:

# returns a map of {obj_id = deque of entries}all_objs=pool.get_all("P1")

Check if pool is registered:

pool.is_registered("P3")

Show debug UI:

pool=ObjectPool("My Pool")
pool.show_ui()

Dependencies


About

ObjectPool implementation that let's you create pools, put objects into them and subscribe for new objects

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages