[Twisted-Python] Do Viewables absolutely have to be return'ed by Avatars?

Glyph glyph at twistedmatrix.com
Wed Dec 4 07:26:29 MST 2013


On Dec 2, 2013, at 11:45 AM, Daniel Sank <sank.daniel at gmail.com> wrote:

>> One way to fix this is to manually construct a ViewPoint rather than a Viewable and pass that to the client.
> 
> For the sake of trying to get my current project to work and so that I
> can understand what's really going on and fix the bugs in pb: how
> would I do the similar thing for a Cacheable? Looking at the source so
> far I'm having trouble figuring out where things like Viewable come
> into play.

This isn't quite as clean as with ViewPoint, but it's not all that bad either.  Basically, you need to inject the correct perspective into the RemoteCacheObserver which is constructed; unfortunately it reads it only from the jellier.

So: fix the jellier :-).

So something like this might do the trick:

class Perspectiveize(Jellyable):
    def __init__(self, perspective, obj):
        self.perspective = perspective
        self.obj = obj
    def jellyFor(self, jellier):
        old = jellier.invoker.serializingPerspective
        jellier.invoker.serializingPerspective = self.perspective
        try:
            return self.obj.jellyFor(jellier)
        finally:
            jellier.invoker.serializingPerspective = old

Untested, but I think it should do the trick.  One caveat though; if the 'obj' (in your case, Cacheable) that you wrap with this is serialized twice within the same top-level call to 'jelly', (i.e. if there are multiple references to it from within a single return message or remote method call) the second time it is serialized, it will simply serialize a reference to the first; this means you have to be careful to be sure that the Perspectiveize object is serialized before the Cacheable itself if the Cacheable is to be serialized at all.

Good luck,

-glyph

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20131204/89326649/attachment.html>


More information about the Twisted-Python mailing list