• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Python,ctypes和mmap

用户头像
it1352
帮助1

问题说明

我想知道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?

正确答案

#1

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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjecg
系列文章
更多 icon
同类精品
更多 icon
继续加载