Created by ZopeOrgSite
. Last modified 1999-11-12 17:05:03.
'_v_MyVariable' volatile attributes are useful for storing state information or chaches. Since they are not synchronized or stored in the ZODB database, they can be modified as much as you like without causing needless transactions which
would bloat your ZODB database and slow down your system with write conflicts for Zope to resolve.
The major limitation of '_v_MyVariable' volatile attributes is that they are not shared between threads. If you need volatile attributes shared between threads, you can use a module level variable. Just remember to use Python locks if necessary to keep your code thread safe.
If you need to initialize you '_v_MyVariable' volatile attributes, the correct place to do so is your classes __setstate__(self,state) method, as detailed by Michael Pelletier on the
Zope-Dev mailing list::
Date: Fri, 12 Nov 1999 11:46:20 -0500
From: Michel Pelletier
To: [email protected]
Cc: [email protected]
Subject: Re: [Zope-dev] Correct place to initialize _v_ attributes
Stuart 'Zen' Bishop wrote:
>
> Is there a method I can initialize _v_ attributes in? __init__ is
> only called on initial object creation, but I also need to initialize
> variables when the object is instantiated from ZODB
Define a __setstate__. This is called when the object is activated
(brought into memory from the object store).
Here is an example of how Catalog wakes up:
def __setstate__(self, state):
""" initialize your brains. This method is called when the
catalog is first activated (from the persistent storage) """
Persistent.__setstate__(self, state)
self.useBrains(self._v_brains)
if not hasattr(self, 'lexicon'):
self.lexicon = Lexicon()
There are a couple things going on here, first, the Persistence
machinery is fired up (your object must subclass Persistent, of course),
second useBrains() initializes the default record class for catalog
result records. third, 'self.lexicon' was added to Catalog in this beta
release, so everyone's old 2.0 Catalogs do not contains lexicons. By
checking for the attr in setstate, you can evolve your objects to
contain new components and attributes. You can also add a class
attribute, of course.