Message from discussion
python destructor
Received: by 10.224.186.20 with SMTP id cq20mr5228115qab.8.1352106460508;
Mon, 05 Nov 2012 01:07:40 -0800 (PST)
Received: by 10.236.79.7 with SMTP id h7mr523278yhe.2.1352106460475; Mon, 05
Nov 2012 01:07:40 -0800 (PST)
Path: gf5ni18188417qab.0!nntp.google.com!c7no6905506qap.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 5 Nov 2012 01:07:40 -0800 (PST)
In-Reply-To: <bb0866bf-ab89-423b-bfff-6f21445fd80d@googlegroups.com>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=130.233.217.169;
posting-account=-3gxZwoAAABWFxXJJfFXmE6PEopHjRYy
NNTP-Posting-Host: 130.233.217.169
References: <bb0866bf-ab89-423b-bfff-6f21445fd80d@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c0bb6827-b9f8-4f79-9f79-4c097c19a48c@googlegroups.com>
Subject: Re: python destructor
From: Ferencik Ioan <ferenciki...@gmail.com>
Injection-Date: Mon, 05 Nov 2012 09:07:40 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Monday, November 5, 2012 8:51:00 AM UTC+2, Ferencik Ioan wrote:
> Hello there folks,
>=20
>=20
>=20
> I have a bit of a special issue.
>=20
> I'll start by disclosing myself for what i am doing. I am a postgraduate =
student and I really have good reasons to do what I am doing. At least i th=
ink so.
>=20
>=20
>=20
> And not the issue.
>=20
> I am building a python web service. This web service has some generic obj=
ects and I use a metaclass to customize the classes.=20
>=20
> Second I use a non-conventional object oriented database, dybase=20
>=20
>=20
>=20
> (http://www.garret.ru/dybase/doc/dybase.html#introduction)
>=20
>=20
>=20
> Now these is a OODBMS claiming to support ACID transactions.
>=20
>=20
>=20
>=20
>=20
> The instances of my objects are recursively organizing themselves into a =
hierarchical tree-like structure. When I make an instance of this object pe=
rsistent dybase actually can recursively save all tree structure.
>=20
> Everything works well here.=20
>=20
>=20
>=20
> I altered the main class situated at the root of my class hierarchy to ac=
tually store inside the__dict__ not the instances of its children but their=
unique ID's. Then when I set a child attribute I create it and instead of =
being stored in the instance the child goes to a database index object. Thu=
s it becomes Universally addressable. The a parent retrieves the child it a=
ctually fetches it from the database.=20
>=20
> In this way I ended up with very small objects.However these objects can =
regenerate the treelike structure as if they were storing there children in=
the __dict__.
>=20
>=20
>=20
> The issue is how to give the instances access to the database and properl=
y handle the opening and closing of the database.
>=20
> It seems futile to me to actually open/close the connection through a con=
text. Because the database is a file it will issue an IO operation on every=
attribute access and we all know __getattribute__ is used extremely often.
>=20
> For this reason I thought the best way would be to wrap the dybase Storag=
e (main class) into a local storage version which would have __del__ method=
.
>=20
> The local Storage is a new style class..it opens the DB file but the __de=
l__ is never called.
>=20
> This is because the Storage class has at least 2 cyclic references.=20
>=20
> So my Storage class never closes the database. I would like this class to=
close the database when it is garbage collected.
>=20
> The class is a Singleton FYI as well but this might not be relevant or ev=
en necessary.
>=20
> So my question is:
>=20
> what s the best way to force __del__ on a singleton that has cyclic refer=
ences. Should i use weakref and alter the original source? Is there a way i=
can force a singleton to garbage collect itself?.
>=20
>=20
>=20
> I am by no means a software engineer so i would appreciate any advice fro=
m some experts on the matter.
>=20
> Thank you in advance.
Just in case somebody is interested:
Because my Storage is a singleton I registered the close() method with atex=
it from the Storage open().
This actually closes the connection. Not sure if this is feasible but it WO=
RKS!
I am using mod_wsgi in daemon mode so I have multithreading issues. If I co=
nfigure the mod_wsgi with one process dybase works correctly.
I have to override the Persistent.store() and make it thread safe using mul=
tiprocessing. This is for ANYONE who uses or plans to use dybase in a web e=
nvironment.