[SciPy-dev] Difficulty creating an example for illustrating memmap.flush()

David Goldsmith d_l_goldsmith@yahoo....
Sun Aug 2 17:00:44 CDT 2009


Hi, folks.  So, I'm trying to devise an example to illustrate memmap.flush().  It didn't work the way I was intuitively expecting (i.e., it didn't appear to do anything at all) so I went looking for a "ready-made" example and found:

http://www.slideshare.net/enthought/python-for-scientific-computing-webinar-may-22-2009

which has an example on Slide 25.  However, here's what I get when I try to duplicate the example:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
win32
>>> import numpy as np
>>> np.version.version
'1.3.0rc2'
>>> q = np.memmap('new_file.dat',mode='w+',shape=(2,5))
>>> q
memmap([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)
>>> # Print out underlying file contents
... # Note: not using iPython, so have to use os.system
...
>>> import os
>>> os.system('type new_file.dat')
          0
>>> # Note: already a little different than Webinar Expl.
...
>>> # Next write ascii value for 'A' (65) into q
...
>>> q[:] = ord('A')
>>> q
memmap([[65, 65, 65, 65, 65],
       [65, 65, 65, 65, 65]], dtype=uint8)
>>> # Do I need to call flush before file is written to?
...
>>> os.system('type new_file.dat')
AAAAAAAAAA0
>>> # No! Does flushing change anything?
...
>>> q.flush()
>>> os.system('type new_file.dat')
AAAAAAAAAA0
>>> # No! Is it because I printed q before checking it on disc?
...
>>> # Start afresh, but don't print memmap before checking it
...
>>> r = np.memmap('new_file2.dat',mode='w+',shape=(2,5))
>>> os.system('type new_file2.dat') # "reproducibility check"
          0
>>> r[:] = ord('A')
>>> os.system('type new_file2.dat') # Checking file on disc immediately
AAAAAAAAAA0
>>> # File is updated without calling flush, indeed without any
... # intervening access to the memmap at all!

What gives?  Is this a bug?  If not, can someone please furnish me with an example that clearly an explicitly illustrates the function (and necessity) of memmap.flush()?

Thanks!

DG


      


More information about the Scipy-dev mailing list