我正在编写一个相当简单的 python 程序来读取 mmap 区域,并且可能会修改该 mmap 区域中的一些字节,或者可能会获取其中一些字节的副本。
mmap 区域有几 MB 大,并分成 2048 字节大的帧。程序在该区域连续循环非常快,有时会进行复制,几乎所有时间都触及每个 2048 字节帧中的几个字节。
为了将对象创建所花费的时间限制到最少(即:仅在需要副本时创建对象),我想为每个 2048 字节帧创建一个指向内存的“某些对象”列表。
到目前为止,我已经设法使用 ctypes(2048 个字符的数组)来做到这一点,但我发现使用起来很麻烦。我想知道是否有一种方法可以从 mmap 对象创建内存视图或类似字节数组的对象,而不是创建内存的实际副本,而是该对象仅指向 mmap。
这是一些代码:
from ctypes import *
import mmap
class fixed_size_frame_2048(Structure):
_fields_ = [("data",c_ubyte * 2048)]
nb_frames = 4096
#mmap initializing code resulting in a map object pointing to shared memory
#the map object is nb_frames*2048 bytes big
map = mmap.mmap(
-1,
2048 * nb_frames,
flags=(mmap.MAP_SHARED),
prot=(mmap.PROT_READ | mmap.PROT_WRITE)
)
.....
#initial creation of frame objects pointing into the mmap area
frames= [ None ] * nb_frames
for i in range(nb_frames) :
frames[i] = fixed_size_frame_2048.from_buffer(self.map,2048*i)
#that I don't manage to make work
#self.frames[i] = memoryview(self.map[2048*i : 2048*(i+1)])
#in the following loop, I never create an object unless I need a copy
position = 0
while True :
if frames[position].data[2] != 0 :
#copy the frame to another object
newcopy = bytearray(frames[position].data)
#do some other stuff with the new object
#....
#write a byte or a few into the mmap area (I'm looking for a more pythonic thing than ctypes here)
frames[position].data[2] = 0
#loop stuff
position += 1
position %= nb_frames
期待阅读任何答案,我被那个答案难住了。我一定遗漏了一些明显的东西,但是什么?
昨晚已经很晚了,所以请注意以下几点:似乎 memoryview 向我返回了 ctype 对象的地址,而不是 ctype 对象指向的内存区域。如果我在 map[:] 切片上运行 bytearray(),我会得到一个副本,如果我在切片上尝试 memoryview,它似乎坚持指向 mmap 的开始。(或至少:对于所有 4096 帧,它始终指向 mmap 区域中的同一位置-)。