[ZODB-Dev] Weak References

Jim Fulton jim@zope.com
Fri, 11 Jan 2002 14:53:28 -0500


Casey Duncan wrote:
> 
> 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).

What prevents you from using it in it's original containment environment?

> In other words you can have the cake (the object), but you can't eat
> it too (use it wrapped with its original container).

This doesn't actually clarify anything, since I really don't know
anything about the cake you want. ;)
 
> 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.

Your problem description is too unclear to offer any help.

> I see that there is a PEP (205) to get weak refs directly in Python, which is
> encouraging.

Python has weak references.

> 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.

The ZODB alreadt does something like this. In fact, circular references
among persistent objects don't cause memory leaks.
 
> 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?

I can't tell what problem you are trying to solve.
 
> Should I shut up and just wait for Python 2.x to support weak references? If
> so, will ZODB4 support them?

Python already has wek references. In fact, in the Zope3 branch, 
I'm using them in the ZODB cache, although I think I'll eventually
switch to a ZODB-specific approach that uses less memory.

Jim

--
Jim Fulton           mailto:jim@zope.com       Python Powered!        
CTO                  (888) 344-4332            http://www.python.org  
Zope Corporation     http://www.zope.com       http://www.zope.org