用于引用计数和与本机指针交互的python包
refcount的Python项目详细描述
参考计数
用于计算本机资源的参考的python包
此包主要用于管理本地库中的资源,例如,用Python编写C++中的资源。虽然它可以归结为“简单地”维护一组计数器,但要正确地执行此操作,而不是最终导致内存泄漏或崩溃,实际上是很复杂的。这个包旨在提供用于管理外部本地资源的结构化选项-我无法找到只做我需要的事情的pypi包。除了本机库资源外,其他需要引用计数的用例可能受益于在refcount
中重用和扩展类。
许可证
麻省理工学院衍生(见License.txt)
安装
pip install refcount
来源:
pip install -r requirements.txt python setup.py install
文档
示例
基于一个单元测试的这个包的使用的规范说明。假设我们有一个带有对象的C++库和一个C API:
#define TEST_DOG_PTR testnative::dog*#define TEST_OWNER_PTR testnative::owner*#define TEST_COUNTED_PTR testnative::reference_counter*testnative::dog*create_dog();testnative::owner*create_owner(testnative::dog*d);voidsay_walk(testnative::owner*owner);voidrelease(testnative::reference_counter*obj);// etc.
从库的外部导出api时使用不透明指针void*
(也可以处理c结构指针和本机c99类型)。
void*create_dog();void*create_owner(void*d);voidsay_walk(void*owner);voidrelease(void*obj);// etc.
从最后一点开始,在python中,我们希望一个api隐藏接近c api的底层细节,特别是避免通过release
c api调用来管理本机内存,而是使用python gc。
dog=Dog()owner=DogOwner(dog)owner.say_walk()print(dog.position)dog=None# the "native dog" is still alive though, as the owner incremented the ref countowner=None
这可以通过refcount
和cffi
包实现。一种可能的设计是:
ut_ffi=cffi.FFI()ut_ffi.cdef('extern void* create_dog();')ut_ffi.cdef('extern void* create_owner( void* d);')ut_ffi.cdef('extern void say_walk( void* owner);')ut_ffi.cdef('extern void release( void* obj);')# etc.ut_dll=ut_ffi.dlopen('c:/path/to/test_native_library.dll',1)# Lazy loadingclassCustomCffiNativeHandle(CffiNativeHandle):def__init__(self,pointer,prior_ref_count=0):super(CustomCffiNativeHandle,self).__init__(pointer,type_id='',prior_ref_count=prior_ref_count)def_release_handle(self):ut_dll.release(self.get_handle())returnTrueclassDog(CustomCffiNativeHandle):def__init__(self,pointer=None):ifpointerisNone:pointer=ut_dll.create_dog()super(Dog,self).__init__(pointer)# etc.classDogOwner(CustomCffiNativeHandle):def__init__(self,dog):super(DogOwner,self).__init__(None)self._set_handle(ut_dll.create_owner(dog.get_handle()))self.dog=dogself.dog.add_ref()# Do note this important reference incrementdefsay_walk(self):ut_dll.say_walk(self.get_handle())def_release_handle(self):super(DogOwner,self)._release_handle()# super(DogOwner, self)._release_handle()self.dog.release()returnTrue
相关工作
祖先
这个Python包^ {CD1>}实际上是从C++、R和.NET之间互用性的前期工作中产生的。Python的端口也受到Kevin Plastow的著作的影响,并在澳大利亚气象局以C/{C++/Python互操作方式使用^ {CD5}}进行。
读者可能还想看看:
- 用于.net/本机互操作的nuget包dynamic-interop-dll。
- 一组主要是c++软件tools for interop with C/C++
- 用于generating interop glue code on top of C API glue code的C库。
其他python包
虽然本程序包是部分编写的,因为没有现有的Python(Python)工作可以很好地满足需要,但是有一些包可以更好地满足您的特定需求:- infi.pyutils构造一个引用计数类。