I am wondering if it is possible for the ctypes package to interface with mmap.
Currently, my module allocates a buffer (with create_string_buffer
) and then passes that using byref
to my libraries mylib.read
function. This, as the name suggests, reads data into the buffer. I then call file.write(buf.raw)
to write the data to disk. My benchmarks, however, show this to be far from optimal (time spent in file.write
is time better spent in mylib.read
).
I am therefore interested in knowing if ctypes can interoperate with mmap. Given an mmap.mmap
instance and an offset how can I get a pointer (c_void_p
) into the address space?
An mmap
object "supports the writable buffer interface", therefore you can use the from_buffer class method, which all ctypes
classes have, with the mmap
instance as the argument, to create a ctypes
object just like you want, i.e., sharing the memory (and therefore the underlying file) that the mmap
instance has mapped. I imagine, in specific, that you'll want a suitable ctypes
array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With