Repository navigation
Conversation
Create a public function close() to properly close mmap See Method to explicitly close memmap #13510, there are two ways to un-mapped a memory-mapped file: a. del fp or b. fp._mmap.close() The first method may leave the file locked for a long period of time. The second method exposes internal variable _mmap. The solution provides public function to close the memmap object.
|
I have tested it again on Windows 10, it did not crash when I call |
|
Please don't recreate the pull request every time you make an addition/modification, |
|
Xref #17940, note the comment about crashing the interpreter there may still apply |
|
This will need a release note. |
Co-authored-by: Bas van Beek <[email protected]>
Co-authored-by: Bas van Beek <[email protected]>
|
I am not familiar with the github GUI. That's why re-submit similar pull requests. Thanks |
Added context manager and close() function. I have created an example to test this features.
Update example using context manager and closed()
Co-authored-by: Bas van Beek <[email protected]>
eric-wieser
left a comment
There was a problem hiding this comment.
As discussed in #13510, this is not safe, and calling close() on a memmap for which a view survives elsewhere causes a segfault.
Co-authored-by: Bas van Beek <[email protected]>
I have tested it, the code work fine. import os.path
import shutil
from tempfile import mkdtemp
import numpy as np
np.random.seed(1234)
data = np.random.uniform(0, 100, 4)
data.resize(4, 1)
tempdir = mkdtemp()
filename = os.path.join(tempdir, 'newfile.dat')
print(f'{filename = }\n')
fp1 = np.memmap(filename, dtype='float64', mode='w+', shape=(4,1))
fp1[:] = data[:]
fp1.flush()
print(f'{fp1 = }\n')
fp2 = np.memmap(filename, dtype='float64', mode='r+', shape=(4,1))
print(f'{fp2 = }\n')
# Close fp1 but fp2 is still pointing to the file
fp1.close()
# Perform write to fp2
fp2[:] = data*data
fp2.flush()
print(f'{fp2 = }\n')
# See what is inside filename using context manager
with np.memmap(filename, dtype='float64', mode='r') as fp3:
print(f'{fp3 = }\n')
# Close fp2 now
fp2.close()
# Delete everything
shutil.rmtree(tempdir)The output is |
That is because you're not running the right test. I expect the one in #13510 (comment) to still fail (after replacing |
Update output from with context manager for unit testing.
|
I have fixed the unit test within the code. However, do not know how to test the code again. import os.path
import shutil
from tempfile import mkdtemp
import numpy as np
np.random.seed(1234)
data = np.random.uniform(0, 100, 4)
data
tempdir = mkdtemp()
filename = os.path.join(tempdir, 'newfile.dat')
print(f'{filename = }\n')
fp1 = np.memmap(filename, dtype='float64', mode='w+', shape=(4,1))
fp1[:] = data[:]
fp1.flush()
print(f'{fp1 = }\n')
fp2 = np.memmap(filename, dtype='float64', mode='r+', shape=(4,1))
print(f'{fp2 = }\n')
# Close fp1 but fp2 is still pointing to the file
fp1.close()
# Perform write to fp2
fp2[:] = data*data
fp2.flush()
print(f'{fp2 = }\n')
# See what is inside filename using context manager
with np.memmap(filename, dtype='float64', mode='r') as fp3:
print(f'{fp3 = }\n')
# Close fp2 now
fp2.close()
# Delete everything
shutil.rmtree(tempdir)I have tested the code. Do not know why my last comment was not shown up here. |
|
See my comment in #13510 |
Create a public function close() to properly close mmap
See Method to explicitly close memmap #13510, there are two ways to un-mapped a memory-mapped file:
a. del fp or
b. fp._mmap.close()
The first method may leave the file locked for a long period of time. The second method exposes internal variable _mmap.
The solution provides public function to close the memmap object.