Python 如何在从pb.Root继承的服务器中检测丢失的客户端连接?
例如,我有一个客户端,它通过以下方式连接到服务器:Python 如何在从pb.Root继承的服务器中检测丢失的客户端连接?,python,twisted,Python,Twisted,例如,我有一个客户端,它通过以下方式连接到服务器: class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory): def __init__(self): pb.PBClientFactory.__init__(self) self.ipaddress = None def clientConnectionMade(self, broker): log.m
class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory):
def __init__(self):
pb.PBClientFactory.__init__(self)
self.ipaddress = None
def clientConnectionMade(self, broker):
log.msg('Started to connect.')
pb.PBClientFactory.clientConnectionMade(self, broker)
def buildProtocol(self, addr):
log.msg('Connected to %s' % addr)
return pb.PBClientFactory.buildProtocol(self, addr)
def clientConnectionLost(self, connector, reason):
log.msg('Lost connection. Reason:', reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason):
log.msg('Connection failed. Reason:', reason)
ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
因此,客户端可以在连接丢失时自动检测
如果客户端出现故障(例如崩溃),如何从服务器获得相同的行为
我目前发现了一个DeadReferenceError(通过迭代潜在连接的客户机列表),但这是一种非事件驱动的方式——坦白说,已经太晚了
欢迎任何意见
提前谢谢
Ben您可以注册一个回调,以便在连接断开时调用。为此有两个API,一个是
Broker.notifyOnDisconnect
,另一个是RemoteReference.notifyOnDisconnect
。它们做同样的事情,但根据应用程序的详细信息,其中一个可能更便于访问
我不确定您是否可以使用它来保证您永远不会得到DeadReferenceError
(例如,我不确定如果调用remote\u foo
方法会发生什么情况如果调用您的方法,您返回一个延迟,连接丢失,然后延迟触发),但在常见情况下,您至少可以大幅降低这种可能性(即,如果在notifyOnDisconnect
调用函数后从未使用callRemote
,您应该能够始终避免从callRemote
获取DeadReferenceError
)