[ZODB-Dev] How do you "wake up" sleepy persistent objects?

Jim Fulton jim@zope.com
Fri, 12 Oct 2001 10:41:35 -0400


This sounds reasonable. The issue isn't really the need to
wake up ghosts, but the poor interaction betweem dir and ZODB.
The problem seems to be that dir makes assumptions about the 
semantics of __dict__. I suspect that this will change a lot in
Python 2.2.  In any case, ZODB (eventually) will be substantially 
rewritten for 2.2 sans ExtensionClass. At this time, we should 
make sure that the new dir works right with the new ZODB. :)

BTW, you might want to think about being more clever to ghostify
objects after you've looked at them, to save memory.

In Zope, whenever we do anything that walks an object tree, we
are careful to check for ghosts so that we can make return objects
to ghostedness when we are done with them. We use code along the
lines of:

def walk(o):

  was_ghost = o._p_changed is None
  
  # do whatever we want to do with o

  ....

  # if o is a container, get subobjects

  for sub in o.objectValues(): walk(sub)

  # Now make it a ghost again, if necessary:
  if was_ghost: o._p_deactivate()


Jim

Greg Ward wrote:
> 
> On 11 October 2001, Jim Fulton said:
> > First, you usually don't need to wake up ghosts. Why would
> > anyone want to wake up a ghost? I can see doing it
> > during debugging, but not otherwise.
> 
> I need to do this in Grouch, which walks an object graph and checks that
> all objects are of "known" classes, have the right set of attributes,
> and each attribute is of the right type.  The code in question is
> generic -- lots of dir(), hasattr(), and getattr(), but not an
> "object.foo" in sight.
> 
> If the object graph in question lives in a ZODB, then the first time we
> hit each object, it's a ghost.  The first call to dir() always fails --
> ie. it always returns [].  It looks like hasattr() and getattr() always
> work right off the bat; it's only dir() that needs prodding.  As it
> happens, my object-checking algorithm starts by calling dir() on the
> object to be checked, to make sure it has the right set of attributes.
> So before I do this "real" dir() call, I do "fake" dir() call to make
> sure the object is awake:
> 
>     def _check_attrs (self, value, klass_def, context):
> 
>         # First, make sure there are no unexpected attributes.  (Doing this
>         # first -- in the instance dictionary's hash order -- means the
>         # *real* type-checking loop, below, is done according to the order
>         # of attributes in the class definition, ie. in a predictable,
>         # canonical order.  This is a good thing, if only because it makes
>         # writing tests easier.)
> 
>         if context.mode == "zodb":
>             # prod ZODB into loading the object before relying on
>             # the value of dir()
>             dir(value)
> 
>         all_ok = 1
>         actual_attrs = dir(value)               # <- this fails without
>         for actual_attr in actual_attrs:        #    the above "prod"
> 
> Sounds like this is pretty similar to Patrick's auto-completion feature,
> although for a completely different purpose.
> 
>         Greg
> --
> Greg Ward - software developer                gward@mems-exchange.org
> MEMS Exchange                            http://www.mems-exchange.org
> 
> _______________________________________________
> For more information about ZODB, see the ZODB Wiki:
> http://www.zope.org/Wikis/ZODB/
> 
> ZODB-Dev mailing list  -  ZODB-Dev@zope.org
> http://lists.zope.org/mailman/listinfo/zodb-dev

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