<div class="gmail_quote">On 17 May 2012 11:13, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, May 17, 2012 at 9:01 AM, Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br>
> A record is an interesting critter -- it is given life either from the user<br>
> or from the disk-bound data; its fields can then change, but those changes<br>
> are not reflected on disk until .write_record() is called; I do this<br>
> because I am frequently moving data from one table to another, making<br>
> changes to the old record contents before creating the new record with the<br>
> changes -- since I do not call .write_record() on the old record those<br>
> changes do not get backed up to disk.<br>
<br>
</div>I strongly recommend being more explicit about usage and when it gets<br>
written and re-read, rather than relying on garbage collection.<br>
Databasing should not be tied to a language's garbage collection.<br>
Imagine you were to reimplement the equivalent logic in some other<br>
language - could you describe it clearly? If so, then that's your<br>
algorithm. If not, you have a problem.<br></blockquote><div><br></div><div>Agreed. To me, this sounds like a perfect case for with: blocks and explicit reference counting. Something like (pseudo-python - not runnable):</div>
<div><br></div><div><div>class Record:</div><div> def __init__(self):</div><div> self.refs = 0</div><div> self.lock = threading.Lock()</div><div> </div><div> def __enter__(self):</div><div> with self.lock:</div>
<div> self.refs += 1</div><div> </div><div> def __exit__(self):</div><div> with self.lock:</div><div> self.refs -=1</div><div><br></div><div> if self.refs == 0:</div><div> self.write_record()</div>
<div><br></div><div> <rest of Record class></div><div><br></div><div>rec = record_weakrefs.get('record_name')</div><div><br></div><div>if rec is None:</div><div> rec = load_record()</div><div> record_weakrefs.put('record_name', rec)</div>
<div><br></div><div>with rec:</div><div> do_stuff</div></div><div><br></div><div>Tim Delaney</div></div>