[Zope-dev] Zope/Python Object Persistence

Chimezie Thomas-Ogbuji cogbuji@fourthought.com
Mon, 01 May 2000 18:41:30 -0600


If you make the dictionary an attribute of the zope object the
getNextCount() method is called on, it should be pickled automatically
by ZODB. You'll need this logic in the beggining of getNextCount:

if (attribute exists):
	increment count attribute
else:
	set attribute to default value (0) and increment

This solution assumes you want a per-object persistent counter. If what
you want is a class-wide counter (common to all instances) then I'm not
sure how you'd do this besides:
1) using ZPickle product (might be overkill)
2) using Persistent List product (might be more appropriate)

> Adam Pawliuk wrote:
> 
> Hi,
> 
> Is there a way that I can maintain persistent python objects in
> memory through the Zope Server?
> 
> As a simple example if I create a counter in a python module and
> access it through an external method, the module seems to get reloaded
> every so often, so the counter will get reset. Can I avoid this?
> Basically I just want to keep a persistent hashtable/Dictionary.
> 
> MODULE:
> *************************************************************
> __persist = {};
> __persist['count'] = 0;
> 
> def getNextCount():
>    __persist['count'] = int( __persist['count'] )+1
>    return __persist['count']
> ************************************************************
> External method in DTML:
> 
> <dtml-var "getNextCount()">
> 
> 
> thanks in advance
>