Python,ctypes和mmap [英] Python, ctypes and mmap

查看:113
本文介绍了Python,ctypes和mmap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道ctypes包是否可以与mmap交互。

I am wondering if it is possible for the ctypes package to interface with mmap.

当前,我的模块分配了一个缓冲区(使用 create_string_buffer ),然后使用 byref 将其传递给我的库 mylib.read 函数。顾名思义,这会将数据读入缓冲区。然后,我调用 file.write(buf.raw)将数据写入磁盘。但是,我的基准测试表明,这远非最佳选择(在 file.write 中花费的时间在 mylib.read )。

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).

因此,我很想知道ctypes是否可以与mmap互操作。给定 mmap.mmap 实例和偏移量,如何将指针( c_void_p )放入地址空间? / p>

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?

推荐答案

mmap 对象支持可写缓冲区接口,因此您可以使用 from_buffer 类方法,该方法都 ctypes 类具有 mmap 实例作为参数,以创建 ctypes 对象,就像共享对象一样,即共享 mmap 实例已映射的内存(并因此共享基础文件)。我想具体来说,您会想要一个合适的 ctypes 数组

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.

这篇关于Python,ctypes和mmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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