wbolster / plyvel

Plyvel, a fast and feature-rich Python interface to LevelDB
https://plyvel.readthedocs.io/
Other
530 stars 75 forks source link

Explicit iterator closing #19

Closed wbolster closed 10 years ago

wbolster commented 10 years ago

The iterator API should allow explicit closing. Ideas:

This issue was inspired by @jtolds in #17:

also, we need to support closing iterators, so that we don't have to wait for garbage collection to kick in to free the leveldb snapshot backing the iterator

wbolster commented 10 years ago

Actually, the current implementation already has an internal Iterator.close(), but it is not accessible from the outside.

wbolster commented 10 years ago

@jtolds, I'd appreciate your take on this.

jtolio commented 10 years ago

whoops, i suck at the internet, sorry for the delay.

honestly, my preference is option 1.

iterator closing isn't necessary in low-load or not-very-memory-constrained environments, so it's not that big of a deal to forget to call it or something. my expectation is the only people who will close iterators are people who are already aware of the snapshot/gc leak problem. so i'm okay with having a sort of explicit interface.

i don't think you want to close the iterator on stopiteration. it feels a little funny. if someone iterates and then wants to call seek again, what happens?

the context manager thing is fine, but yeah as you pointed out that's the same as @contextlib.closing

wbolster commented 10 years ago

I've just implemented option 1 and 3 from the original report in commit 53e65ad. Updated docs describe how it works:

@jtolds Please review the docs, and feel free to come up with better text suggestions.

wbolster commented 10 years ago

i don't think you want to close the iterator on stopiteration. it feels a little funny. if someone iterates and then wants to call seek again, what happens?

I agree this is funny. Specifying an autoclose=True flag is just as much typing as it.close(), but in the latter case it is much more obvious what happens. Explicit is better than implicit. :)

jtolio commented 10 years ago

this all looks great, thanks a lot wouter

wbolster commented 10 years ago

Ok, great. I've just released Plyvel 0.6: https://plyvel.readthedocs.org/en/0.6/news.html#plyvel-0-6