How to use a contiguous memory location of n bytes in python
Nick Craig-Wood
nick at craig-wood.com
Fri Nov 14 08:29:58 EST 2008
bearophileHUGS at lycos.com <bearophileHUGS at lycos.com> wrote:
> chachi:
> > I want to know how to instantiate a data structure which has n bytes
> > (given by me) and is internally stored in a contiguous fashion.
>
> array.array("B", ...) may be fit for you. You can also use a numpy
> array of bytes.
The mmap module is useful also for larger amounts (>4k say).
mmap's are individually free-able so they don't fragment your memory.
http://docs.python.org/library/mmap.html
Eg create a 1 GB anonymous mmap, access it and then delete it
>>> from mmap import mmap
>>> a = mmap(-1, 1000000000)
>>> a[0]
'\x00'
>>> a[0] = 'z'
>>> a[999999999]
'\x00'
>>> a[999999999]='q'
>>> a[999999999]
'q'
>>> del a
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list