Using numpy.memmap to map a device file

Is there a reason why opening a device file (not a regular file) with numpy memmap should not work?

self.surface = np.memmap('/dev/fb1', dtype=np.uint16, mode='r+', shape=(320,240))

I work with a custom kernel module that adds a framebuffer device that works fine with the regular mmappython module . But using numpy seems to hang the kernel mutex when accessing the file system or something like that (I'm really not sure what exactly is happening).

My question here specifically is what numpy memmap cannot handle, and should I go the other way?

I asked another question about unix stackexchange , but it seems to me that these are two different questions, so I posted both of them.

Obviously this is on linux (kubuntu maverick with custom kernel module)

Update

Well, it turns out I can create memmap perfectly. The problem is that when I close the process without specifically closing the memmap object, it will just hang in the mutex in the kernel.

I don't know if this problem was with numpy, or my kernel module, or somewhere else.

+3
source share
1 answer

If your code works fine with the python module mmap, you can use it directly instead numpy.memmap:

>>> fd = os.open("a", os.O_RDWR)
>>> buffer = mmap.mmap(fd, 0)
>>> surface = np.ndarray((320,240), np.uint16, buffer)

This has another advantage in that you have more control over the memory mapping used.

, python mmap . , msync . , ? (, buffer.flush(), msync). call close() , , , msync!

+1

Source: https://habr.com/ru/post/1791909/


All Articles