In Python, is it possible to overload Numpy's memmap to delete itself when the memmap object is no longer referenced?
Question: I am trying to use memmap when certain data doesn't fit in memory and employ
memmap's ability to trick code into thinking it's just an ndarray. To further
expand on this way of using memmap I was wondering if it would be possible to
overload memmap's dereference operator to delete the memmap file.

So for example:

    
    
    from tempfile import mkdtemp
    import os.path as path
    filename = path.join(mkdtemp(), 'tmpfile.dat')
    {
        out = np.memmap(filename, dtype=a.dtype, mode='w+', shape=a.shape)
    }
    # At this point out is out of scope, so the overloaded 
    # dereference function would delete tmpfile.dat
    

Does this sound feasible/has this been done? Is there something I am not
thinking of?

Thank you!


Answer: just delete the file after it has been opened by `np.memmap` the file will
then be deleted by the system after the last reference to the file descriptor
is closed.

python temporary files work like this and can very conveniently be used with
the `with` context manger construct:

    
    
    with tempfile.NamedTemporaryFile() as f:
        # file gone now from the filesystem 
        # but f still holds a reference so it still exists and uses space (see /prof<pid>/fd)
        # open it again (will not work on windows)
        x = np.memmap(f.name, dtype=np.float64, mode='w+', shape=(3,4))
    # f reference is gone now but x still has a reference
    del x
    # now all references are gone and the file is properly deleted
    

