On Tue, Aug 3, 2010 at 3:21 PM, David Andrews <dammi...@gmail.com> wrote:
> I want to create a lazy seq backed by an open file (or db connection, > or something else that needs cleanup). I can't wrap the consumer in a > with-anything. > > Is there a general method for cleaning up after the consumer discards > its reference to that lazy seq? I'm vaguely aware of Java finalize, > but am also aware that it is unpredictable (e.g. you aren't guaranteed > to be driven at the next gc). > > Does Clojure even provide the ability to define a finalize method for > a lazy seq? > > (Sipping water from a firehose...) > > The descriptor(/db connection/etc) will be cleaned up eventually, when the finalizer runs. The problem with this is that you don't know when the finalizer will run, and it can be arbitrarily delayed. So it's possible for a program to run out of file descriptors, if there are file descriptors which are garbage but not yet collected. Another possibility, if you know that the consumer will force the entire list before discarding it, is to concat on a zero-element list which closes the descriptor, like: (concat orig-list (lazy-seq (do (.close fd) []))) On the other hand, the consumer doesn't force the whole list, then the descriptor never gets closed. So the real answer is: this isn't a good use for seqs. It looks like one, but it isn't. Brian -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en