Skip to content

ENH: Close memmap file with a public function close() - #17941

Closed
yoonghm wants to merge 10 commits into
numpy:masterfrom
yoonghm:patch-3
Closed

yoonghm wants to merge 10 commits into
numpy:masterfrom
yoonghm:patch-3

Conversation

@yoonghm

@yoonghm yoonghm commented Dec 6, 2020

Copy link
Copy Markdown

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.

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.
@yoonghm

yoonghm commented Dec 6, 2020

Copy link
Copy Markdown
Author

I have tested it again on Windows 10, it did not crash when I call fp.close().

@BvB93

BvB93 commented Dec 6, 2020

Copy link
Copy Markdown
Member

Please don't recreate the pull request every time you make an addition/modification,
just push the respective change.

@BvB93 BvB93 linked an issue Dec 6, 2020 that may be closed by this pull request
@eric-wieser

Copy link
Copy Markdown
Member

Xref #17940, note the comment about crashing the interpreter there may still apply

Comment thread numpy/core/memmap.py Resolved
@charris

charris commented Dec 8, 2020

Copy link
Copy Markdown
Member

This will need a release note.

Co-authored-by: Bas van Beek <[email protected]>
Comment thread numpy/core/memmap.py Resolved
Co-authored-by: Bas van Beek <[email protected]>
@yoonghm

yoonghm commented Dec 8, 2020

Copy link
Copy Markdown
Author

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.
Comment thread numpy/core/memmap.py Resolved
yoonghm and others added 2 commits December 10, 2020 01:02
Update example using context manager and closed()
Co-authored-by: Bas van Beek <[email protected]>
@BvB93 BvB93 added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Dec 9, 2020
Comment thread numpy/core/memmap.py Resolved
Comment thread numpy/core/memmap.py Resolved

@eric-wieser eric-wieser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@yoonghm yoonghm closed this Dec 10, 2020
@yoonghm yoonghm reopened this Dec 10, 2020
@yoonghm

yoonghm commented Dec 10, 2020

Copy link
Copy Markdown
Author

As discussed in #13510, this is not safe, and calling close() on a memmap for which a view survives elsewhere causes a segfault.

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

filename = 'C:\\Users\\yoonghm\\AppData\\Local\\Temp\\tmpdgsiz4pb\\newfile.dat'

fp1 = memmap([[19.15194504],
        [62.2108771 ],
        [43.7727739 ],
        [78.53585837]])

fp2 = memmap([[19.15194504],
        [62.2108771 ],
        [43.7727739 ],
        [78.53585837]])

fp2 = memmap([[ 366.79699873],
        [3870.19323005],
        [1916.05573496],
        [6167.88105013]])

fp3 = memmap([ 366.79699873, 3870.19323005, 1916.05573496, 6167.88105013])

@eric-wieser

Copy link
Copy Markdown
Member

I have tested it, the code work fine.

That is because you're not running the right test. I expect the one in #13510 (comment) to still fail (after replacing _mmap.close with close)

Update output from with context manager for unit testing.
@yoonghm

yoonghm commented Dec 10, 2020

Copy link
Copy Markdown
Author

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.

@yoonghm

yoonghm commented Dec 10, 2020

Copy link
Copy Markdown
Author

See my comment in #13510

@yoonghm yoonghm closed this Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

01 - Enhancement 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method to explicitly close memmap

4 participants