Exclude a field from serialization

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Exclude a field from serialization

Hector Blanco-2
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
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exclude a field from serialization

Souheil CHELFOUH
you can prefix it with _v_

2011/8/3 Hector Blanco <[hidden email]>:

> 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
> [hidden email]
> https://mail.zope.org/mailman/listinfo/grok-dev
>
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exclude a field from serialization

Leonardo Rochael Almeida
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 <[hidden email]> wrote:

> you can prefix it with _v_
>
> 2011/8/3 Hector Blanco <[hidden email]>:
>> 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
>> [hidden email]
>> https://mail.zope.org/mailman/listinfo/grok-dev
>>
> _______________________________________________
> Grok-dev mailing list
> [hidden email]
> https://mail.zope.org/mailman/listinfo/grok-dev
>
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exclude a field from serialization

Sylvain Viollon

Op 4 aug 2011, om 00:18 heeft Leonardo Rochael Almeida het volgende geschreven:

  Hello,

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

  Actually since the connection will probably be already close (I hope for you) it
  is more like to have directly errors.

  Regards,

  Sylvain,


--
Sylvain Viollon -- Infrae
t +31 10 243 7051 -- http://infrae.com
Hoevestraat 10 3033GC Rotterdam -- The Netherlands



_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exclude a field from serialization

Hector Blanco-2
Thank you all for your thorough replies...

I'm going to take a look to zc.async, see if I can get it working.
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exclude a field from serialization

Souheil CHELFOUH
You can have a look at my old grok project, Snappy, that uses a
zc.async solution
http://gitweb.dolmen-project.org/

might give you some ideas

2011/8/4 Hector Blanco <[hidden email]>:
> Thank you all for your thorough replies...
>
> I'm going to take a look to zc.async, see if I can get it working.
> _______________________________________________
> Grok-dev mailing list
> [hidden email]
> https://mail.zope.org/mailman/listinfo/grok-dev
>
_______________________________________________
Grok-dev mailing list
[hidden email]
https://mail.zope.org/mailman/listinfo/grok-dev