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
原创粉丝点击