简体   繁体   English

如何在龙卷风中设置连接超时?

[英]how to set connection timeout in tornado?

In my Tornado app in some situation some clients disconnect from server but my current code doesn't detect that client is disconnect from server. 在某些情况下,在我的Tornado应用程序中,某些客户端与服务器断开连接,但是我当前的代码无法检测到客户端与服务器断开连接。 I currently use ping to find out if client is disconnected. 我目前使用ping来查找客户端是否断开连接。 here is my ping pong code: 这是我的乒乓代码:

from threading import Timer
class SocketHandler(websocket.WebSocketHandler):
    def __init__(self, application, request, **kwargs):
        # some code here
        Timer(5.0, self.do_ping).start()
    def do_ping(self):
        try:
            self.ping_counter += 1
            self.ping("")
            if self.ping_counter > 2:
                self.close()
            Timer(60, self.do_ping).start()
        except WebSocketClosedError:
            pass

    def on_pong(self, data):
        self.ping_counter = 0

now I want to set SO_RCVTIMEO in tornado instead of using ping pong method. 现在我想在龙卷风中设置SO_RCVTIMEO而不是使用乒乓方法。 something like this : 像这样的东西:
sock.setsockopt(socket.SO_RCVTIMEO)
Is it possible to set SO_RCVTIMEO in Tornado for close clients from server after specific time out ? 在特定的超时后,是否可以在Tornado中设置SO_RCVTIMEO来关闭服务器上的客户端?

SO_RCVTIMEO does not do anything in an asynchronous framework like Tornado. SO_RCVTIMEO在像Tornado这样的异步框架中不做任何事情。 You probably want to wrap your reads in tornado.gen.with_timeout . 您可能希望将读取的内容包装在tornado.gen.with_timeout You'll still need to use pings to test the connection and make sure it is still working; 您仍然需要使用ping来测试连接并确保其仍在工作; if the connection is idle there are few guarantees about how long it will take for the system to notice. 如果连接空闲,则几乎无法保证系统需要花费多长时间。 (TCP keepalives are a possibility, but these are not configurable on all platforms and generally use very long timeouts). (TCP保持连接是可能的,但是并非在所有平台上都可以配置,并且通常使用很长的超时时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM