在 python 中使用 mmap 函数需要的示例
我正在寻找 python 中的示例来内存映射 1gb 文件。有人有我可以使用的例子吗?
该文件可以是任何随机文本。我只是想看看什么是正确的方法来做到这一点......
I am seeking an example in python to memory map a 1gb file. Does anyone have an example I can use?
The file can be any random text. I just want to look to see what is the proper way to do this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个可以帮助您理解 python (3.0+) 中的 mmap 的示例
下面的代码打开一个文件,然后对其进行内存映射。它执行映射文件的 readline() 方法,证明它的工作方式与标准文件一样。然后,它读取和写入映射文件的切片(访问映射文件内容的同等有效方法,不会更改文件指针)。最后,文件指针重新定位在开头,并读入(更新的)内容。(“14”是 write() 函数的返回值,它始终返回写入的字节数。)
Here is an example that can help you understand mmap in python (3.0+)
The code below opens a file, then memory maps it. It exercises the readline() method of the mapped file, demonstrating that it works just as with a standard file. It then reads and writes slices of the mapped file (an equally valid way to access the mapped file's content, which does not alter the file pointer). Finally the file pointer is re-positioned at the start and the (updated) contents are read in. (The "14" is the return value of the write() function, which always returns the number of bytes written.)