How to stop a thread?

M marvelan at hotmail.com
Thu Oct 10 15:17:47 EDT 2002


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

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
way.


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

        while not magical_flag:
            self.server.handle_request()



More information about the Python-list mailing list