Getting data from ctypes array into numpy

前端 未结 6 797
执念已碎
执念已碎 2020-11-27 02:53

I am using a Python (via ctypes) wrapped C library to run a series of computation. At different stages of the running, I want to get data into Python, and spec

6条回答
  •  北海茫月
    2020-11-27 03:39

    Another possibility (which may require more recent versions of libraries than is available when the first answer was written -- I tested something similar with ctypes 1.1.0 and numpy 1.5.0b2) is to convert from the pointer to the array.

    np.ctypeslib.as_array(
        (ctypes.c_double * array_length).from_address(ctypes.addressof(y.contents)))
    

    This seems to still have the shared ownership semantics, so you probably need to make sure that you free the underlying buffer eventually.

提交回复
热议问题