Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, ctypes and mmap

Tags:

python

ctypes

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?

like image 334
Freddie Witherden Avatar asked Sep 03 '10 22:09

Freddie Witherden


1 Answers

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.

like image 151
Alex Martelli Avatar answered Sep 22 '22 12:09

Alex Martelli