如何检测 Linux 上 Twisted 的非正常断开?
我写了一个基于Twisted的服务器,我遇到了一个问题,一些客户端没有正常地断开连接。例如,用户拔掉网线。
有一段时间,Windows上的客户端断开了(调用了connectionLost,也是用Twisted写的)。而在Linux服务器端,我的twisted的connectionLost永远不会被触发。即使它在连接丢失时尝试将数据写入客户端。为什么Twisted无法检测Linux上的非正常断开连接(甚至向客户端写入数据)?如何让 Twisted 检测非正常断开连接?因为 Twisted 功能无法检测非优雅,所以我的服务器上有很多僵尸用户。
---- 更新 ----
我认为这可能是类unix操作系统的套接字的特性,那么,类unix操作系统上的套接字处理这种情况的行为是什么?
谢谢。 维克多·林.
I wrote a server based on Twisted, and I encountered a problem, some of the clients are disconnected not gracefully. For example, the user pulls out the network cable.
For a while, the client on Windows is disconnected (the connectionLost is called, and it is also written in Twisted). And on the Linux server side, my connectionLost of twisted is never triggered. Even it try to writes data to client when the connection is lost. Why Twisted can't detect those non-graceful disconnection (even write data to client) on Linux? How to makes Twisted detect non-graceful disconnections? Because the feature Twisted can't detect non-graceful, I have lots of zombie user on my server.
---- Update ----
I thought it might be the feature of socket of unix-like os, so, what is the behavior of socket on unix-like for handling situation like this?
Thanks.
Victor Lin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
支持Jean-Paul所说的,如果你需要更细粒度的TCP连接管理,只需使用reactor.CallLater。我们在 Twisted/wxPython 交易平台上就有了这样的实现,而且效果很好。您可能还想调整 ReconnectingClientFactory 的行为,以实现我理解您所寻找的结果。
Seconding what Jean-Paul said, if you need more fine grained TCP connection management, just use reactor.CallLater. We have exactly that implementation on a Twisted/wxPython trading platform, and it works a treat. You might also want to tweak the behaviour of the ReconnectingClientFactory in order to achieve the results I understand your looking for.
您正在描述不可靠网络上 TCP 连接的行为。 Twisted 只是暴露了这种行为:毕竟,当您与 Twisted 建立 TCP 连接时,它只不过是一个 TCP 连接。
当您说 < 时,您就错了em>connectionLost 即使您尝试通过它发送数据,回调也不会被调用。两分钟后,底层 TCP 连接将消失,Twisted 将通过调用 连接丢失。
如果您需要更快地检测这种情况,那么您可以使用 reactor.callLater。
You're describing the behavior of TCP connections on an unreliable network. Twisted is merely exposing this behavior: after all, when you set up a TCP connection with Twisted, it is nothing more than a TCP connection.
You're mistaken when you say that the connectionLost callback isn't invoked even if you try to send data over it. After two minutes, the underlying TCP connection will disappear and Twisted will inform you of this by calling connectionLost.
If you need to detect this condition more quickly than that, then you can implement your own timeouts using reactor.callLater.