I used a descriptor to make sure that the memory address is always updated.
class NpDescriptor:
def __init__(self, dtype):
self.dtype = dtype
self.current_address = 0
self.current_buffer = 0
self.current_size = 0
def __get__(self, instance, owner):
cdef:
size_t current_address_new=instance._get_vector_address()
size_t current_size_new=len(instance)
object haystack_buffer
if current_address_new != self.current_address or current_size_new != self.current_size:
haystack_buffer = (self.dtype * (current_size_new)).from_address(current_address_new)
self.current_address=current_address_new
self.current_buffer = np.frombuffer(haystack_buffer,dtype=self.dtype)
self.current_size = current_size_new
return self.current_buffer
def __set__(self, instance, value):
instance.__dict__[
self.name] = np.array([],self.dtype)
In the official documentation, there is another way, I haven't tried it yet.