Killing multi-threaded SocketServer

I'm trying to figure out why I can't kill my multi threaded SocketServer via a CRTL-C.

Basically I have that :

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

I'm trying to understand what i've done wrong and what would be the best way to be able to kill the whole script via a crtl-c. Any help would be greatly appreciated !

Thanks

Answers


Here is a solution:

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()

To close the server you can type return or close the stdin.

The Problem is with the Thread class that will not allow closing the application before all Threads are closed.

serve_forever() will not end until you close the belonging to server(an other solution) on KeyboardInterrupt.


When creating threads, set them as daemon :

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

In this way all the thread will terminate when you have killed the main thread.

Based on python documentation in here :

A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property.


Need Your Help

How to use Facebook OAuth within Android native?

android facebook sdk oauth native

I'm using official Facebook Android SDK to integrate Facebook to my application. When it comes to authorize, the SDK use a custom dialog with Webview in it, loading facebook login page for user to ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.