On 17 May 2012 11:13, Chris Angelico <ros...@gmail.com> wrote:

> On Thu, May 17, 2012 at 9:01 AM, Ethan Furman <et...@stoneleaf.us> wrote:
> > A record is an interesting critter -- it is given life either from the
> user
> > or from the disk-bound data;  its fields can then change, but those
> changes
> > are not reflected on disk until .write_record() is called;  I do this
> > because I am frequently moving data from one table to another, making
> > changes to the old record contents before creating the new record with
> the
> > changes -- since I do not call .write_record() on the old record those
> > changes do not get backed up to disk.
>
> I strongly recommend being more explicit about usage and when it gets
> written and re-read, rather than relying on garbage collection.
> Databasing should not be tied to a language's garbage collection.
> Imagine you were to reimplement the equivalent logic in some other
> language - could you describe it clearly? If so, then that's your
> algorithm. If not, you have a problem.
>

Agreed. To me, this sounds like a perfect case for with: blocks and
explicit reference counting.  Something like (pseudo-python - not runnable):

class Record:
    def __init__(self):
        self.refs = 0
        self.lock = threading.Lock()

    def __enter__(self):
        with self.lock:
            self.refs += 1

    def __exit__(self):
        with self.lock:
            self.refs -=1

            if self.refs == 0:
                self.write_record()

    <rest of Record class>

rec = record_weakrefs.get('record_name')

if rec is None:
    rec = load_record()
    record_weakrefs.put('record_name', rec)

with rec:
    do_stuff

Tim Delaney
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to