views:

20

answers:

1

Hi!

I have program which has servers interacting with each other using Twisted's remote procedure calls, and I run in problems with closing connections when they are not needed anymore. Connections should be able to close itself in both sides.

Case 1: How do I close connection in connecting part?

factory = pb.PBClientFactory()
reactor.connectTCP(ip, port, factory)
deferred = factory.login(credentials.UsernamePassword(username, password), client=self)
deferred.addCallbacks(self.connectedToServer, self.errorConnectingToServer)

def connectedToServer(self, server):
  self.server = server
  # Closing connection comes here

Case 2: How do I close connection in server part?

class MyPerspective(pb.Avatar):

  def connected(self, server):
    self.client = server
    # Closing connection comes here

At the moment I use raising pb.Error() to close connection, but I don't think that's the proper way to do it.

A: 

Another option is reference.broker.transport.loseConnection().

RemoteReference instances which are created over a PB connection are given a broker attribute. The broker attribute refers to the protocol instance that created them. As usual for a protocol, the broker has a transport attribute, and the transport has a loseConnection method.

Jean-Paul Calderone