Resources and non-exhausted generators
Hamish Lawson
hbl at st-andrews.ac.uk
Thu May 2 11:03:10 EDT 2002
I wrote:
> I've written a generator ... that returns results from a database.
> ... [I]f the generator is not run to completion, then the cursor and
> connection will not get properly closed.
Emile van Sebille replied:
> Since you're doing a fetchall, couldn't you restructure to fetchall and
> cursor.close before iterating?
You are quite correct if I use fetchall. I was mistakenly regarding
fetchall as an iterator (I've had iterators and generators on the brain
recently!), but of course it actually creates a list. Since my
motivation for using an iterator was to avoid that kind of memory
overhead, let's assume that I actually use fetchone inside a loop
instead. Would my observation still hold about the cursor and connection
not getting closed?
Hamish Lawson
More information about the Python-list
mailing list