简体   繁体   English

Python,ctypes和mmap

[英]Python, ctypes and mmap

I am wondering if it is possible for the ctypes package to interface with mmap. 我想知道ctypes包是否可以与mmap连接。

Currently, my module allocates a buffer (with create_string_buffer ) and then passes that using byref to my libraries mylib.read function. 目前,我的模块分配一个缓冲区(使用create_string_buffer ),然后使用byref将其传递给我的库mylib.read函数。 This, as the name suggests, reads data into the buffer. 顾名思义,这将数据读入缓冲区。 I then call file.write(buf.raw) to write the data to disk. 然后我调用file.write(buf.raw)将数据写入磁盘。 My benchmarks, however, show this to be far from optimal (time spent in file.write is time better spent in mylib.read ). 然而,我的基准测试表明这远远不是最佳的(在file.write中花费的时间是在file.write上花费的mylib.read )。

I am therefore interested in knowing if ctypes can interoperate with mmap. 因此,我有兴趣知道ctypes是否可以与mmap互操作。 Given an mmap.mmap instance and an offset how can I get a pointer ( c_void_p ) into the address space? 给定一个mmap.mmap实例和一个偏移量我如何获得指针( c_void_p )到地址空间?

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, ie, sharing the memory (and therefore the underlying file) that the mmap instance has mapped. mmap对象“支持可写缓冲区接口”,因此您可以使用from_buffer类方法(所有ctypes类都具有mmap实例作为参数)来创建ctypes对象,就像共享内存一样(因此mmap实例已映射的底层文件。 I imagine, in specific, that you'll want a suitable ctypes array . 我想,具体来说,你需要一个合适的ctypes 数组

Be aware that the operating system is going to be doing readahead for read() anyway. 请注意,无论如何操作系统都将为read()做预读。 You're going to be blocking either in read() or write()--one or the other will bottleneck the operation--but even though you're blocking in one, that doesn't mean the other isn't taking place for you behind the scenes. 你将在read()或write()中阻塞 - 一个或另一个会阻塞操作 - 但即使你在一个阻塞,这并不意味着另一个没有发生在幕后为你服务。 That's the job of every multitasking operating system. 这是每个多任务操作系统的工作。

If you use mmap for this, you're very likely making things more complicated for the OS--making it harder for it to determine that you're really just streaming data in and out, and making it more complicated for it to do read-ahead. 如果您使用mmap,那么很可能会让操作系统变得更复杂 - 这使得它更难以确定您实际上只是将数据流入和流出,并使其更难以进行读取-先。 It may still figure it out (operating systems are very good at this), but you're probably not helping. 它仍然可以解决(操作系统非常擅长),但你可能没有帮助。

The only benefit in principle is avoiding the cost of a memory copy, but it doesn't sound like that's your goal here (and unless profiling clearly says otherwise, I'd strongly doubt that would help performance). 原则上唯一的好处是避免了内存复制的成本,但这听起来并不像你的目标(除非分析清楚地说明,否则我强烈怀疑这会有助于提高性能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM