python - How do I check if a twisted.internet.protocol instance has disconnected -
i create connection server this:
connection = tcp4clientendpoint(reactor, server_host, server_port) factory = factory() factory.protocol = protocol protocol = yield connection.connect(factory) protocol.dosomething() # returns deferred
now, in other method, have handle on protocol object want test if protocol still connected, like:
if protocol.isconnected(): dosomethingelse()
is there way this. looked @ twisted documentation , couldnt find relevant method. setting flag in connectionlost() callback option, wondering if avoid doing that.
twisted tries light possible when comes stored state. bare factories keep absolutely no track of children, protocols
know little themselves. callback bags.
setting flag in connectionlost()
method way it. future reference:
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
edit: note there's reason feels uncomfortable. if have method needs ask question, working outside callback chain. if running code exclusively within life-cycle methods exposed protocol
, may not need this.
Comments
Post a Comment