杀死多线程SocketServer

时间:2012-09-08 09:16:57

标签: python multithreading python-2.7 kill

我试图找出为什么我不能通过CRTL-C杀死我的多线程SocketServer。

基本上我有:

import SocketServer,threading

class TEST(SocketServer.BaseRequestHandler):
    def server_bind(self):
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR,SO_REUSEPORT, 1)
        self.socket.bind(self.server_address)
        self.socket.setblocking(0)

    def handle(self):
        request, socket = self.request
        data = request
        if data[0] == "\x01":
           buff = "blablabla"
           socket.sendto(str(buff), self.client_address)

class TEST1(SocketServer.BaseRequestHandler):
    def server_bind(self):
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR,SO_REUSEPORT, 1)
        self.socket.bind(self.server_address)
        self.socket.setblocking(0)

    def handle(self):
        request, socket = self.request
        data = request
        if data[0] == "\x01":
           buff = "blablabla"
           socket.sendto(str(buff), self.client_address)

class TEST2(SocketServer.BaseRequestHandler):
    def server_bind(self):
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR,SO_REUSEPORT, 1)
        self.socket.bind(self.server_address)
        self.socket.setblocking(0)

    def handle(self):
        request, socket = self.request
        data = request
        if data[0] == "\x01":
           buff = "blablabla"
           socket.sendto(str(buff), self.client_address)

class TEST3(SocketServer.BaseRequestHandler):
    def server_bind(self):
        self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR,SO_REUSEPORT, 1)
        self.socket.bind(self.server_address)
        self.socket.setblocking(0)

    def handle(self):
        request, socket = self.request
        data = request
        if data[0] == "\x01":
           buff = "blablabla"
           socket.sendto(str(buff), self.client_address)

def serve_thread_udp(host, port, handler):
    server = SocketServer.UDPServer((host, port), handler)
    server.serve_forever()

def serve_thread_tcp(host, port, handler):
    server = SocketServer.TCPServer((host, port), handler)
    server.serve_forever()

def main():
    try:
      threading.Thread(target=serve_thread_tcp,args=('', 4045,TEST)).start()
      threading.Thread(target=serve_thread_tcp,args=('', 239,TEST1)).start()
      threading.Thread(target=serve_thread_udp,args=('', 1246,TEST2)).start()
      threading.Thread(target=serve_thread_tcp,args=('', 12342,TEST3)).start()
    except KeyboardInterrupt:
        os._exit()

if __name__ == '__main__':
    try:
        main()
    except:
        raise

我试图了解我做错了什么以及能够通过crtl-c杀死整个脚本的最佳方法。 任何帮助将不胜感激!

由于

2 个答案:

答案 0 :(得分:1)

这是一个解决方案:

def main():
    import thread
    try:
      thread.start_new(serve_thread_tcp, ('', 4045,TEST))
      thread.start_new(serve_thread_tcp,('', 239,TEST1))
      thread.start_new(serve_thread_udp,('', 1246,TEST2))
      thread.start_new(serve_thread_tcp,('', 12342,TEST3))
    except KeyboardInterrupt:
        os._exit()

if __name__ == '__main__':
    try:
        main()
    except:
        raise
    raw_input()

要关闭服务器,您可以键入return或关闭stdin。

问题在于Thread类在关闭所有Threads之前不允许关闭应用程序。

在您关闭KeyboardInterrupt上的所属服务器(另一个解决方案)之前,

serve_forever()不会结束。

答案 1 :(得分:1)

创建线程时,将它们设置为守护程序:

Thread.__init__(self)
self.setDaemon(True)

这样,当你杀死主线程时,所有线程都会终止。

基于here中的python文档:

  

线程可以标记为“守护程序线程”。这个标志的意义在于,当只剩下守护进程线程时,整个Python程序都会退出。初始值继承自创建线程。可以通过守护程序属性设置标志。