[Twisted-Python] ReconnectingClientFactory.stopTrying

Eric C. Newton ecn at metaslash.com
Fri May 2 19:06:58 EDT 2003

RedHat 9.0
Twisted 1.0.4

I have two servers, A and B, connected over a network which may, or
may not be working well.  Server A wants to send a bunch of data over
to Server B.  I'm using ReconnectingClientFactory to get A connected
to B even in the presence of failures.

Once Server A dumps the data on B, it should disconnect and go back to
doing it's (unrelated) work, and close the connection on B.

ReconnectingClientFactory has "stopTrying" which I call to prevent
future connections from being made.  Then I close the current
connection.  Unfortunately that tells the ReconnectingClientFactory
that it should reconnect, even though it should have stopped trying.
Shortly thereafter I get a stack trace.  Reversing the
order... loseConnection, stopTrying, doesn't work either.

Test code below will give a stack trace after a few seconds of

My (present) workaround is to close the connection, and tell the
ReconnectingClientFactory to stopTrying with:

	reactor.callLater(0.1, self.factory.stopTrying)

It works, but the 0.1 is a kludge to allow connection clean-up to
finish, but timed to go off before the Connector starts a new attempt.

from twisted.internet import reactor
from twisted.internet.protocol import Factory, ReconnectingClientFactory
from twisted.protocols.basic import Int16StringReceiver
import pickle

class In(Int16StringReceiver):

    def __init__(self):
        self.msgs = {}
    def stringReceived(self, msg):
        n, msg = pickle.loads(msg)
        self.msgs[n] = msg

    def connectionLost(self, reason):
        print self.msgs.values()

class Out(Int16StringReceiver):

    def __init__(self):
        self.msgs = {}

    def connectionMade(self):
        for i in range(10):
            self.msgs[i] = 'X' * i
        for i in self.msgs.keys():
            self.sendString(pickle.dumps( (i, self.msgs[i])))
    def stringReceived(self, msg):
        n = pickle.loads(msg)
        del self.msgs[n]
        if not self.msgs:

f = Factory()
f.protocol = In

c = ReconnectingClientFactory()
c.protocol = Out

reactor.connectTCP('localhost', PORT, c)
reactor.listenTCP(PORT, f)

More information about the Twisted-Python mailing list