[Twisted-Python] pb.setFactoryForClass()

Sean Riley sean at twistedmatrix.com
Sat Jun 15 14:24:59 EDT 2002


proposal for new functionality in pb:

In addition to the existing pb.setCopierForClass() I'd like a
pb.setFactoryForClass() which would take a factory method instead of a class
as its argument.

This would allow serialized objects to be re-constructed on the other end of
network connections by a factory instead of just be creating a new class.

I have a use case where objects are created locally on both the client and
server and these objects need to be passed as arguments to remote method
calls. The best way to do it seems to be to send a unique ID over the wire
and have a factory method transform the ID into the correct object on the
other side.

A trivial example:

class Thing(pb.Copyable)
  allIDs = {}

  def __init__(self, id):
    self.id = id
    Thing.allIDs[id] = self

  def getStateToJellyFor(self, perspective):
    return {"id":self.id}

def thingFactory(state):
  return Thing.allIDs.get(state["id"],None)

pb.setFactoryToCopyFor("Thing", thingFactory)


Things could then be send over the network as very small objects (just IDs)
as long as both side have already loaded all the "Things" from local
storage.

----
"If it's not running programs or fusing atoms, it's just bending space.",
Ken Macleod
"That's it, I'm outta here.", Homer Simpson's Brain
Sean Riley
sean at ninjaneering.com





More information about the Twisted-Python mailing list