-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Iterators and resource management #22466
Comments
What's wrong with just manually doing the clean up? We should have better support for function local resource cleanup but I don't think that should be in the iterator protocol at all. |
It's too high overhead to add try/finally around every try/catch But it is occasionally a useful pattern that I've used a couple times (for example, https://github.com/JuliaLang/julia/blob/master/base/weakkeydict.jl#L121) |
@yuyichao How? The resource is allocated in
@vtjnash I don't understand, there is no
Yes, that kind of works, but not really. Good example is when I open a file in the |
Change the API. |
There are cases in base that would benefit from a solution to this problem (e.g. here), and it seems generally a useful pattern. Note that e.g. in .Net |
For that case, In general, I don't think the resource management should be bound to iterator since the And as I said in the first comment, other than this, I don't think there's anything different from any other resource management so we should fix just that. |
That would mean that you can't have any lazy iterator story if any of your iterators depends on a non-managed resources. That would exclude countless really useful scenarios (especially in the file IO and DB access scenario) and a pattern that is used successfully a lot on other platforms. |
Why is that? Just do FWIW, the proposed API is not better than just closing the resource when the iteration is done. If it is a resource only needed for iteration, you don't need it after done returns true. If it is a resource referenced by iterator output, the loop isn't the correct scope to close it (the correct scope is only known by the user). |
What if the user of the iterator never iterates everything? Then the final |
Is a finalizer still the best solution/workaround for this issue? BTW, I looked at the |
This is a problem I have in Query.jl: I sometimes want to iterate things where I am acquiring some resource in the
start
method that needs to be explicitly freed later on. For example I might open a file in thestart
method, and at some point I'd like to close it again. I could close it in the last call to thedone
function, but I can't be sure that it will ever be called, so that is really kind of sloppy.One solution might be that a normal
for
loop gets lowered to something like this:With a default implementation of
dispose
:And then I could add a method to
dispose
for my type that closes say a file that I opened instart
.That design would probably only work if the compiler was able to detect cases where the whole
try...finalize
block did nothing (i.e. where thefinally
block didn't do anything) and then was able to remove the wholetry...finally
construct. I assume otherwise it would cause too much of a performance problem.In any case, even if my idea here doesn't work, it would be good to have some resource management story for iterators.
Probably relates to #18823 and #11207.
The text was updated successfully, but these errors were encountered: