简体   繁体   English

如何重启 TCP socketserver?

[英]How to restart a TCP socketserver?

I have written a code for handling multiple requests at a time but my server freezes randomly after a while, is there any way to check and restart the server if there is no activity.我已经编写了一次处理多个请求的代码,但是我的服务器在一段时间后随机冻结,如果没有活动,有没有办法检查并重新启动服务器。

class MyTCPHandler(socketserver.BaseRequestHandler):

    def handle(self):
        self.data = self.request.recv(1024).strip()
        #doing some work and generating response
        self.request.sendall(response)

if __name__ == "__main__":
    HOST, PORT = "0.0.0.0", 4879

    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
        
        try:
            server.serve_forever()
        except KeyboardInterrupt:
            server.shutdown()
            server.socket.close()

via bash if you want to stop the server ps aux|grep The name of the program|awk '{print $2}'|xargs kill通过 bash 如果要停止服务器ps aux|grep The name of the program|awk '{print $2}'|xargs kill

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

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