[ZODB-Dev] Weak References

Casey Duncan c.duncan@nlada.org
Fri, 11 Jan 2002 14:21:23 -0500


I've going up oneside and down the other trying to figure out a better way 
for Zope objects to robustly refer to one another. I am constantly battling 
either Acquisition or Circular references (which the former is meant to 
alleviate).

Acquisition is all swell and quite convenient, but sometimes you need to 
store a persistent direct reference to an object. This in and of itself is 
easy. The hard part is doing that and having the ability to use the object in 
its original containment environment (for lack of better terminology on my 
part). In other words you can have the cake (the object), but you can't eat 
it too (use it wrapped with its original container).

I have come to the conclusion that there is not general application level way 
to make this work. So, that leads me here. Maybe there is something that can 
be done at the database level to help. I have a general solution, but it 
creates a large amount of circular references, so I think that a system level 
weak reference implementation would be very helpful. 

I see that there is a PEP (205) to get weak refs directly in Python, which is 
encouraging. I also saw several other implementations. I was wondering though 
how hard it would be for me to implement such a thing leveraging the ZODB 
storage cycle as a trigger.

I figure that in memory, the object references could be the same as regular 
references. The application would not know better. However, when the object 
was removed from memory, the ZODB would drop references marked as weak and 
eliminate those objects no longer referenced. When the object was reloaded 
the __setstate__ or what have you would reestablish the references to those 
objects that still exist. Any weak references made to since garbage collected 
objects would no longer be valid and would raise an exception when accessed.

So I guess my questions are: 

Am I right that an application level solution is not practical and if so what 
could be done at the DB level to facilitate this? 

Should I shut up and just wait for Python 2.x to support weak references? If 
so, will ZODB4 support them?

/---------------------------------------------------\
  Casey Duncan, Sr. Web Developer
  National Legal Aid and Defender Association
  c.duncan@nlada.org
\---------------------------------------------------/