mmap - how to use ?

David Bolen db3l at fitlinxx.com
Thu Nov 16 13:23:20 EST 2000


"June Kim" <junaftnoon at nospamplzyahoo.com> writes:

> Beos 2.0 and tried 1.6 as well but to fail.
> Have you succeeded anything with mmap?

I haven't had to use mmap yet within Python, but I just tried under
Windows NT4 with Python 2.0 and it seemed to work fine:

(file.input is a 8000000 byte file of 78 character lines of 'A'.  The
 two output lines with "(...)" in them were edited manually for readability
 in this post - the missing information are just more 'A's)

    >>> import mmap
    >>> file = open('file.input','r+')
    >>> map = mmap.mmap(file.fileno(),0)
    >>> len(map)
    8000000
    >>> map.readline()
    'AAAAAAAAAAA (...) AAAAAAAA\015\012'
    >>> map.readline()
    'AAAAAAAAAAA (...) AAAAAAAA\015\012'
    >>> map[0:10]
    'AAAAAAAAAA'
    >>> 

Using a size of 0 lets mmap determine the file size, although it will
limit itself to the maximum Python integer, so I'm not sure if you
could map a file that was larger than 2GB.

If you set a size that is smaller than the file, you'll only see that
portion of the file.  If you set a size larger than the actual file
size, it's size will be extended (and Windows seems to fill the space
with NULs).

It's important to open the file for R/W mode since that's the mode
that it will be mapped in - otherwise you'll get an access denied
exception.  Unlike Unix, Python under Windows doesn't let you control
the access of the mapping.

One of the earlier posts in this thread mentioned receiving an error
87 from Windows.  That indicates an invalid parameter in an API call,
which would seem to imply it didn't like something within the module
in terms of how the mapping was being done.  But it would be helpful
to see all the errors and the precise manner in which mmap was being
called to know for sure.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list