使用numpy.memmap映射设备文件 [英] using numpy.memmap to map a device file

查看:312
本文介绍了使用numpy.memmap映射设备文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用numpy的memmap打开设备文件(而不是常规文件)的原因?

Is there a reason that opening a device file (rather than a regular file) using numpy's memmap shouldn't work?

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

我正在使用一个自定义内核模块,该模块添加了一个帧缓冲设备,该设备可以与python的常规mmap模块一起正常工作.但是使用numpy似乎会使内核的互斥锁挂在访问文件系统之类的东西上(我真的不确定到底是怎么回事).

I'm working with a custom kernel module that adds a framebuffer device, which works fine with python's regular mmap module. But using numpy seems to hang the kernel's mutex on accessing the filesystem or something (I'm really not sure exactly what's happening).

我在这里的问题是numpy的memmap无法处理的,我应该采取其他方式吗?

My question here is specifically is this something that numpy's memmap can't handle and I should go a different way?

我在 unix stackexchange 上问了另一个问题,但我觉得这是2种不同的情况问题,所以我都把它们都贴了.

I've asked another question on unix stackexchange, but I feel like it's 2 different questions so I've posted them both.

很明显,这是在linux上(带有自定义内核模块的Kubuntu特立独行的)

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

更新:

好吧,事实证明我可以创建memmap很好.看来问题是当我关闭进程而不专门关闭memmap对象时,它将挂在内核中的互斥体上.

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

我不知道这个问题是否与numpy,我的内核模块或其他地方有关.

I have no idea if this issue is with numpy, or my kernel module, or somewhere else.

推荐答案

如果您的代码可以在python mmap模块上正常运行,则可以直接使用它代替numpy.memmap:

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

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

这还有另一个优点,即您可以更好地控制所使用的内存映射.

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

现在,python的mmap有其自身的特点.如所示,它会在delallocation上调用msync.也许这是您的程序挂起的地方? (您也许可以使用buffer.flush()重现您的问题,该事件也称为msync).您首先调用close()的解决方案可能会起作用,因为它绕过了msync!

Now, python's mmap has its own peculiarities. As the source shows, it calls msync on delallocation. Maybe this is where your program hangs? (You might be able to reproduce your issue with buffer.flush(), which also calls msync). Your solution of calling close() first probably works because it circumvents msync!

这篇关于使用numpy.memmap映射设备文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆