[Grok-dev] Exclude a field from serialization

Leonardo Rochael Almeida leorochael at gmail.com
Wed Aug 3 18:18:57 EDT 2011


Hi Hector,

As Souheil said, a ._v_timer attribute won't be serialized. The
flipside is that it can disappear at any moment, so you can't rely on
it actually being there. _v_ attributes should only be used as caches
for potentially expensive calculations or external connections, in a
pattern like:

class Foo(...):

    _v_cached_value = None

    def getSomeValue(self)
        if self._v_cached_value is None:
            self._v_cached_value = self._doExpensiveCalculation()
        return self._v_cached_value

And only access self.getSomeValue(), never _v_cached_value directly
(unless you do the equivalent "if ... is None" dance).

The other problem I see with your approach is that you're using a new
thread, even if indirectly through threading.Timer(). If you access
ZODB persistent objects in a thread differently than the one where you
got them you Will Have Trouble (TM).

If you need to fire some sort of background or delayed activity, I
suggest you take a look at zc.async:

http://pypi.python.org/pypi/zc.async

On Wed, Aug 3, 2011 at 20:00, Souheil CHELFOUH <trollfot at gmail.com> wrote:
> you can prefix it with _v_
>
> 2011/8/3 Hector Blanco <white.lists at gmail.com>:
>> Hello everybody
>>
>> I have a Grok application serialized in ZoDB. I want each of the
>> containers to have an instance of a Timer. Something like:
>>
>> class BaseManager(grok.Container):
>>        def __init__(self, queriedClass):
>>                super(BaseManager, self).__init__()
>>                self.queriedClass = queriedClass
>>                self.__myTimer = threading.Timer(60, self.timerExecute)
>>
>> That timer (the __poster field) doesn't need to be serialized in the
>> database. It probably even can't, because when I try to create the
>> application I get.
>>
>>
>> Module ZODB.serialize:422 in serialize
>>>>  return self._dump(meta, obj.__getstate__())
>> Module ZODB.serialize:431 in _dump
>>>>  self._p.dump(state)
>> Module copy_reg:70 in _reduce_ex
>>>>  raise TypeError, "can't pickle %s objects" % base.__name__
>> TypeError: can't pickle instancemethod objects
>>
>> (looks like it's trying to serialize the self.timerExecute field)
>>
>> Is there any way to exclude a field from serialization? Thank you in advance.
>> _______________________________________________
>> Grok-dev mailing list
>> Grok-dev at zope.org
>> https://mail.zope.org/mailman/listinfo/grok-dev
>>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev
>


More information about the Grok-dev mailing list