Memory Mapped Files Using Python

Overview:

  • A memory-mapped file is a region of memory that is mapped to a disk file.
  • The mechanism involved in memory-mapped files is the same mechanism used by operating systems for implementing virtual memory.
  • With memory mapped files programs access the data directly from the memory rather than using any I/O routines.
  • While the usage of typical I/O APIs and their overhead is eliminated in using Memory Mapped Files, there are other things like "write order" by multiple programs to be taken care by the programmer.

 

Creating memory mapped files in Python:

  • The Python mmap constructor creates a memory-mapped file.
  • When files are dealt as memory mapped files, the contents of the files can be accessed in the same way a region of memory is accessed, from a Python Program.
  • A memory mapped file can be accessed similar to the way a bytearray is accessed in Python-using subscript notation or using the slice notation.

    mappedFile[pos] = 65

    mappedFile[5:10] = b'Hello'

  • Python mmap constructor has variants for both Windows and Unix systems.
  • To create a memory mapped file the following information needs to be provided to the mmap constructor.
    • A file descriptor
    • Number of bytes of the file to be mapped
    • Offset of the bytes from the beginning of the file
    • Access level - ACCESS_READ for read only, ACCESS_WRITE for read and write and ACCESS_COPY for copy-on-write.
  • When the file descriptor is specified as -1 an anonymous mmap is created that is a memory region, which is not backed by a disk file.

 

Example:

import mmap

 

# Create a memory map backed by a file

fileObject  = open("sample.txt", "r+b")

fileDesc    = fileObject.fileno()

memoryMap   = mmap.mmap(fileDesc, 32)

 

# Print the contents using subscript notation

print(chr(memoryMap[0]))

print(chr(memoryMap[1]))

 

# Print the contents using slicing notation

print(memoryMap[2:5]) 

 

# This will produce an IndexError: mmap index out of range

print(memoryMap[32])


Copyright 2018 © pythontic.com