[Twisted-Python] Clean pb solution for two-way object sync?
Daniel Sank
sank.daniel at gmail.com
Thu Sep 26 22:48:14 MDT 2013
Upon re-reading the Cacheable docs I still don't understand how to use
it. Can we take my previous home-brewed example and use that as a
launch point for illustrating how to use Cacheable? Here's the
example.
We assume the client has a PlayerClient instance with .server pointing
to a PlayerServer instance. The PlayerServer has a .client list of
PlayerClients. The code fragments are the methods needed to spawn a
new Agent and hook up the client and server objects.
class PlayerServer(pb.referenceable):
def newAgent(self, asker):
"""Client told us it wants a new Agent"""
if self.thisIsLegal():
a = AgentServer()
self.agents.append(a)
for client in self.clients:
d = client.callRemote("newAgent", a)
d.addCallback(lambda obj: a.clients.append(obj))
class PlayerClient(bp.referenceable):
def requestNewAgent(self):
"""Tell the server we want to spawn a new Agent"""
self.server.callRemote("newAgent", self)
def newAgent(self, serverObj):
a = AgentClient()
self.agents.append(a)
a.server = serverObj
return a
How does Cacheable help do this?
More information about the Twisted-Python
mailing list