Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem using with to read a file #168

Closed
nden opened this issue Aug 6, 2015 · 4 comments · Fixed by #407
Closed

problem using with to read a file #168

nden opened this issue Aug 6, 2015 · 4 comments · Fixed by #407
Labels

Comments

@nden
Copy link
Contributor

nden commented Aug 6, 2015

@mdboom Should I not use with to open asdf files? Or am I doing something wrong?

ar = np.arange(36).reshape((6,6))
f = AsdfFile()
f.tree = {'regions': ar, 'reftype': 'regions'}
f.write_to('test.asdf')
with AsdfFile.open('test.asdf') as f:
    reg=f.tree['regions']
print reg
<array (unloaded) shape: [6, 6] dtype: int64>

@mdboom
Copy link
Contributor

mdboom commented Aug 6, 2015

You can use a with block, but it needs to be active as long as you need access to the data. (The data is memmapped and the memmap handles are closed at the same time as the file handle). You can also use an explicit close if that's easier.

@nden
Copy link
Contributor Author

nden commented Aug 6, 2015

Right. I knew that. Sorry about the noise.

@nden nden closed this as completed Aug 6, 2015
@embray
Copy link
Contributor

embray commented Aug 18, 2015

I think there should still be more control over this kind of thing. PyFITS deliberately leaves the mmaps open because it's an implementation detail which most users won't even tend to understand, and not allowing this to work I think discourages use of the with statement by users in many cases (for read-only applications it doesn't matter, but there are plenty of write applications where it would be good to use with).

Of course, PyFITS has the opposite problem where users think they've closed the file but they really haven't. I think both should have some way to control this better (but I lean toward leaning mmaps open except when the user has explicitly requested to close them as well).

@embray embray reopened this Aug 18, 2015
@mdboom
Copy link
Contributor

mdboom commented Aug 20, 2015

Yeah -- there's sort of no perfect option here. A kwarg to indicate whether to close the mmaps automatically might be helpful, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants