How to stop a thread?

M marvelan at
Thu Oct 10 21:17:47 CEST 2002

How can I stop a thread that is waiting to handle a TCP

As there is no kill thread function in Python my first idea 
was to do a handle_request until I set a global magical_flag. 

But then I realised that this works only when I have a steady
flow of connections comming in. Otherwise it will just hang
(as it should) in handle_request and thus the thread would
never stop.

So, how can I stop the thread in the example below? I would
like to stop it when user presses the "stop" button in some

class Foo(Thread):
    def run(self):
        self.server = SocketServer.ThreadingTCPServer(addr, h)

        while not magical_flag:

More information about the Python-list mailing list