python 5-4 如何将文件映射到内存mmap.mmap(f.fileno(),0,access=mmap.ACCESS_WRITE)
来源:互联网 发布:8哥皮具淘宝店铺 编辑:程序博客网 时间:2024/05/16 00:17
python 5-4 如何将文件映射到内存
解决方案
使用标准库中mmap模块的mmap函数,它需要一个打开文件描述符作为参数
Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])
#文件描述符获取方法
1.os.open() 2.f=open("test.txt",20) f.fileno ==>获得文件描述符
使用dd 创建大小为1M的文件
dd if=/dev/zero of=test.bin bs=1024 count=1024od -x test.bin 0000000 0000 0000 0000 0000 0000 0000 0000 0000*4000000
import os import mmapf=open("test.bin","r+b")m=mmap.mmap(f.fileno(),0,access=mmap.ACCESS_WRITE)m[0]='\x88'm[4:8]='\xad'*4m2=mmap.mmap(f.fileno(),mmap.PAGESIZE * 8,access=mmap.ACCESS_WRITE,offset=mmap.PAGESIZE * 4)m2[:0x1000]='\xaa'* 0x1000#########################################################od -x test.bin0000000 0088 0000 adad adad 0000 0000 0000 00000000020 0000 0000 0000 0000 0000 0000 0000 0000*0040000 aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa*0050000 0000 0000 0000 0000 0000 0000 0000 0000*4000000
help(mmap.mmap)
Help on class mmap in module mmap:class mmap(__builtin__.object) | Windows: mmap(fileno, length[, tagname[, access[, offset]]]) | | Maps length bytes from the file specified by the file handle fileno, | and returns a mmap object. If length is larger than the current size | of the file, the file is extended to contain length bytes. If length | is 0, the maximum length of the map is the current size of the file, | except that if the file is empty Windows raises an exception (you cannot | create an empty mapping on Windows). | | Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]]) | | Maps length bytes from the file specified by the file descriptor fileno, | and returns a mmap object. If length is 0, the maximum length of the map | will be the current size of the file when mmap is called. | flags specifies the nature of the mapping. MAP_PRIVATE creates a | private copy-on-write mapping, so changes to the contents of the mmap | object will be private to this process, and MAP_SHARED creates a mapping | that's shared with all other processes mapping the same areas of the file. | The default value is MAP_SHARED. | | To map anonymous memory, pass -1 as the fileno (both versions). | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __delitem__(...) | x.__delitem__(y) <==> del x[y] | | __delslice__(...) | x.__delslice__(i, j) <==> del x[i:j] | | Use of negative indices is not supported. | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __len__(...) | x.__len__() <==> len(x) | | __mul__(...) | x.__mul__(n) <==> x*n | | __rmul__(...) | x.__rmul__(n) <==> n*x | | __setitem__(...) | x.__setitem__(i, y) <==> x[i]=y | | __setslice__(...) | x.__setslice__(i, j, y) <==> x[i:j]=y | | Use of negative indices is not supported. | | close(...) | | find(...) | | flush(...) | | move(...) | | read(...) | | read_byte(...) | | readline(...) | | resize(...) | | rfind(...) | | seek(...) | | size(...) | | tell(...) | | write(...) | | write_byte(...) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T
0 0
- python 5-4 如何将文件映射到内存mmap.mmap(f.fileno(),0,access=mmap.ACCESS_WRITE)
- mmap方法将文件映射到内存
- mmap把文件映射到内存
- 文件内存映射(mmap)
- 内存映射文件(MMAP)
- mmap 文件映射内存详解
- Linux内存映射: mmap
- Linux内存映射: mmap
- mmap内存映射操作
- linux mmap 内存映射
- mmap内存映射
- mmap 共享 内存映射
- linux-内存映射mmap
- linux mmap 内存映射
- Linux内存映射: mmap
- linux 内存映射mmap
- Linux mmap内存映射
- linux mmap 内存映射
- 十四年磨一剑,万能数据库查询分析器7.024版本 发布
- 数据结构-线性表(2)
- Linux I/O 调度方法
- Tomcat入门
- 关于eclipse Mars以上版本(Mars 以及NEON)写DOM的时候没有javascript提示的问题
- python 5-4 如何将文件映射到内存mmap.mmap(f.fileno(),0,access=mmap.ACCESS_WRITE)
- php通过http_user_agent判断用户设备类型(脑残版)
- 支付宝集五福建议优化方案
- 朝代歌
- offline chrome installer
- 正则表达式(简单易懂篇)
- 【OTT】【IPTV】三大通信运营商2016年IPTV、OTT招标一览
- 编程思想读书笔记第一章对象导论
- DIV CSS Sprites精灵 CSS图像拼合 CSS背景贴图定位教程案例