How to check if an instance of twisted.internet.protocol is disabled

I create my connection to the server as follows:

connection = TCP4ClientEndPoint(reactor, server_host, server_port)
factory = Factory()
factory.protocol = Protocol
protocol = yield connection.connect(factory)
protocol.doSomething()     # returns a deferred

Now, in some other method, where I have a handle to this protocol object, I want to check if the protocol is all connected, for example:

if protocol.isConnected():
    doSomethingElse()

Is there any way to do this. I looked at the twisted documentation and could not find the appropriate method. Setting a flag in the connectionLost () callback is an option, but I was wondering if I could avoid this.

+5
source share
1 answer

Twisted tries to be as light as possible when it comes to a saved state. Just as bare factories do not watch their children, Protocolsthey know little about themselves. They are mainly callback bags.

connectionLost() - . :

from twisted.internet.protocol import Protocol

class StatefulProtocol(Protocol):
    def __init__(self, factory):
        self.connected = False

    def connectionMade(self):
        self.connected = True

    def connectionLost(self, reason):
        self.connected = False

: , , . , , , , . , Protocol, .

+6

All Articles