The arrayfrombuffer Python package makes it possible to memory-map files as Numerical Python arrays. It contains two modules:

There is some documentation for arrayfrombuffer and maparray available for online perusal, and the package is free software and available for download under an X11-style license.


Memory-mapping files as arrays has the following advantages.

In theory, you could also use this package for things like arrays in memory shared between programs or arrays in distributed shared memory. I haven't tried using it for those things, though.

Someone built a package called "Vmaps" that does something similar, but provides nice things like atomic compare-and-swap for shared-memory arrays. It was released 2002-01-22.


Using it is very easy. To create the array file on disk:

    open('', 'wb').write(somenumericarray.tostring())

To load it back in as type 'l', flattened:

    import maparray
    myarray, mymmapobj, myfile = maparray.maparray('')

Now myarray is a perfectly ordinary Numeric array whose data just happens to be stored in the file ''.

If you want a different data type, you can specify it:

    myarray, mymmapobj, myfile = maparray.maparray('', 'f')
    myarray, mymmapobj, myfile = maparray.maparray('', typecode='f')

You can specify a shape as well:

    myarray, mymmapobj, myfile = maparray.maparray('', 'f', (-1, 24))
    myarray, mymmapobj, myfile = maparray.maparray('', shape=(-1, 24))

If you make changes via myarray and you want them reflected in the file before you delete all references to the mmap object and the array, do this:



It might be nice to have some of the following features: | Kragen's software | Kragen's home page