[Numpy-discussion] np.array, copy=False and memmap

Nisoli Isaia nisoli at im.ufrj.br
Mon Aug 7 17:01:33 EDT 2017

Dear all,
I have a question about the behaviour of

y = np.array(x, copy=False, dtype='float32')

when x is a memmap. If we check the memmap attribute of mmap

print "mmap attribute", y._mmap

numpy tells us that y is not a memmap.
But the following code snippet crashes the python interpreter

# opens the memmap
with open(filename,'r+b') as f:
      mm = mmap.mmap(f.fileno(),0)
      x = np.frombuffer(mm, dtype='float32')

# builds an array from the memmap, with the option copy=False
y = np.array(x, copy=False, dtype='float32')
print "before", y

# closes the file
print "after", y

In my code I use memmaps to share read-only objects when doing parallel
and the behaviour of np.array, even if not consistent, it's desirable.
I share scipy sparse matrices over many processes and if np.array would
make a copy
when dealing with memmaps this would force me to rewrite part of the sparse
Would it be possible in the future releases of numpy to have np.array
if copy is false, if y is a memmap and in that case return a full memmap
instead of slicing it?

Best wishes

P.S. A longer account of the issue may be found on my university blog

Isaia Nisoli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170807/2ad31c9a/attachment-0001.html>

More information about the NumPy-Discussion mailing list