关闭 Twisted 中的非活动连接

编程入门 行业动态 更新时间:2024-10-28 13:26:10
本文介绍了关闭 Twisted 中的非活动连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 LineReceiver 协议运行 Twisted 服务器.有时客户端会悄悄断开连接,因此 Twisted 保持连接打开.并且因为服务器不会发送任何东西,除非请求它,所以永远不会有 TCP 超时.换句话说,有些连接永远不会在服务器端关闭.

I'm running a Twisted server with the LineReceiver protocol. Sometimes clients will disconnect silently, so Twisted keeps the connection open. And because the server doesn't send anything unless requested of it, there's never a TCP timeout. In other words, some connections are never closed server-side.

如何让 Twisted 关闭几个小时不活动的连接?

How can I have Twisted close a connection that's been inactive for a few hours?

推荐答案

您可以使用 reactor.callLater 安排定时事件.基于此,有一个用于向协议添加超时的帮助程序,twisted.protocols.policies.TimeoutMixin.

You can schedule timed events using reactor.callLater. Based on this, there's a helper for adding timeouts to protocols, twisted.protocols.policies.TimeoutMixin.

另一种方法是使用 TCP 保持连接,您可以使用传输的 setTcpKeepAlive 方法启用它.

Another approach is to use TCP keep-alives, which you can enable using the transport's setTcpKeepAlive method.

另一种方法是使用应用程序级保活.基本上是偶尔发送一个noop".它不需要回应.如果连接已经丢失,发送缓冲区中的额外数据最终会导致 TCP 堆栈注意到.

And another approach is to use application-level keep-alives. Essentially send a ''noop'' once in a while. It doesn't need a response. If the connection has been lost, the extra data in the send buffer will cause the TCP stack to eventually notice.

另请参阅常见问题解答.

这篇关于关闭 Twisted 中的非活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 16:25:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1192042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Twisted

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!